问题描述
今天调试flutter程序时报错。程序运行时报如下错误: type 'Future<int>' is not a subtype of type 'int' in type cast
错误源码
int order = DatabaseHelper.dbhelper.getTaskGroupRelationOrder() as int;
TaskGroupRelation relation = TaskGroupRelation(
id:0,
taskId:snapshot.data?[index].id,
groupId:widget.groupId,
taskOrder:order,
);
DatabaseHelper.dbhelper.insertTaskGroupRelationData(relation);
if (context.mounted) Navigator.of(context).pop();
问题分析
类型“Future<int>”不是类型强制转换中类型“int”的子类型。这里不能强制转换,要拿到DatabaseHelper.dbhelper.getTaskGroupRelationOrde()返回结果可以使用DatabaseHelper.dbhelper.getTaskGroupRelationOrder().then((value) =>{})
解决方法
使用DatabaseHelper.dbhelper.getTaskGroupRelationOrder().then((value) =>{})
修改后的代码
TaskGroupRelation relation;
DatabaseHelper.dbhelper.getTaskGroupRelationOrder().then((value) =>
{
relation = TaskGroupRelation(
id:0,
taskId:snapshot.data?[index].id,
groupId:widget.groupId,
taskOrder:value,
),
DatabaseHelper.dbhelper.insertTaskGroupRelationData(relation),
if (context.mounted) Navigator.of(context).pop(),
});
标签:int,cast,dbhelper,relation,DatabaseHelper,type,id
From: https://blog.51cto.com/u_15777557/8602125