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的精彩世界!

相关推荐
你听得到113 小时前
排查 App 问题:别只盯着报错,把前后发生的事情串起来
android·前端·flutter
天空之城--4 小时前
近一周Android Flutter行业动态与实用参考
android·flutter
GitLqr18 小时前
玩转 Flutter 项目依赖:从 pubspec.yaml 到实战包管理
flutter·面试·全栈
程序员老刘1 天前
Qwen 3.8 max干了20分钟没干完,免费模型3分17秒搞定,问题出在哪?
flutter·ai编程
飞凌嵌入式1 天前
告别多端重复开发:Buildroot+Flutter实现全平台UI风格统一
flutter·ui
FungLeo1 天前
Flutter 带 TTL 的多级缓存设计:内存+磁盘+网络三层实战
网络·flutter·缓存·性能优化
FungLeo1 天前
Flutter 可复用公共组件库设计与落地:AppDialog/BottomSheet 等实战
flutter
Fungleo1069972 天前
Flutter/Android Release 包连不上网?AndroidManifest INTERNET 权限排查实录
flutter
GitLqr2 天前
Flutter 实战:为你的 App 增加桌面快捷方式 (Quick Actions)
flutter·app·全栈
FungLeo2 天前
Flutter 把第三方 UI 库渐进迁回 Material 3:组件映射总表 + 四批次替换实战
flutter·ui