Flutter开发type ‘Future<int>‘ is not a subtype of type ‘int‘ in type cast错误

文章目录

问题描述

今天有个同事调试flutter程序时报错,问我怎么解决,程序运行时报如下错误:

type 'Future' 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(),
                            });

结束语 Flutter是一个由Google开发的开源UI工具包,它可以让您在不同平台上创建高质量、美观的应用程序,而无需编写大量平台特定的代码。我将学习和深入研究Flutter的方方面面。从基础知识到高级技巧,从UI设计到性能优化,欢饮关注一起讨论学习,共同进入Flutter的精彩世界!

相关推荐
初遇你时动了情6 小时前
flutter vscode 终端无法使用fvm 版本切换、项目运行
ide·vscode·flutter
一叶难遮天1 天前
Dart语言之面向对象
flutter·面向对象·dart·方法·mixins·泛型·抽象类
汤面不加鱼丸1 天前
flutter实践:DropdownButton2使用示例
flutter
心随雨下1 天前
Flutter Material 3设计语言详解
javascript·flutter·设计语言
猫林老师1 天前
Flutter for HarmonyOS开发指南(六):测试、调试与质量保障体系
flutter·华为·harmonyos
stringwu1 天前
Flutter DevTools 全景介绍
flutter
GISer_Jing1 天前
跨平台Hybrid App开发实战指南
android·flutter·react native
猫林老师2 天前
Flutter for HarmonyOS开发指南(八):国际化与本地化深度实践
flutter·华为·harmonyos
dragon7252 天前
FutureProvider会刷新两次的问题研究
前端·flutter