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

相关推荐
装不满的克莱因瓶1 天前
【2026最新最全】Android Studio安装教程
android·ide·flutter·app·android studio·移动端
西西学代码1 天前
Flutter---通用子项的图片个数不同(2)
flutter
song5011 天前
鸿蒙 Flutter 语音交互进阶:TTS/STT 全离线部署与多语言适配
分布式·flutter·百度·华为·重构·electron·交互
克喵的水银蛇1 天前
Flutter 通用进度条组件:ProgressWidget 一键实现多类型进度展示
flutter
庄雨山1 天前
Flutter 与开源鸿蒙 底部弹窗多项选择实现方案全解析
flutter·开源·openharmonyos
笨小孩7871 天前
Flutter深度解析:从核心原理到实战开发全攻略
flutter
笨小孩7871 天前
Flutter深度解析:从原理到实战的跨平台开发指南
flutter
飛6791 天前
Flutter 状态管理深度实战:从零封装轻量级响应式状态管理器,告别 Provider/Bloc 的臃肿与复杂
前端·javascript·flutter
tangweiguo030519871 天前
Flutter iOS 风格弹框组件封装
flutter
LYFlied1 天前
浅谈跨端开发:大前端时代的融合之道
前端·flutter·react native·webview·大前端·跨端开发·hybrid