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

相关推荐
墨客希15 分钟前
通俗易懂的理解Vue.js
vue.js·flutter
—Qeyser1 小时前
Flutter bottomNavigationBar 底部导航栏
flutter
ZFJ_张福杰2 小时前
【Flutter】APP的数据安全(基于Flutter 交易所APP的总结)
flutter·web3·区块链·app·交易所
Bryce李小白3 天前
Flutter 自定义 View 权威指引
flutter
恋猫de小郭3 天前
Fluttercon EU 2025 :Let‘s go far with Flutter
android·开发语言·flutter·ios·golang
SoaringHeart5 天前
Flutter进阶:自定义一个 json 转 model 工具
前端·flutter·dart
许泽宇的技术分享5 天前
Flutter + Ollama:开启本地AI的全平台新纪元 —— 从零剖析一款现代化AI客户端的技术奥秘
人工智能·flutter
molihuan5 天前
开源 全平台 哔哩哔哩缓存视频合并 Github地址:https://github.com/molihuan/hlbmerge_flutter
android·flutter·缓存·ffmpeg·开源·github·音视频
dora5 天前
Flutter中dart和原生代码的通信之MethodChannel
android·flutter
brave7236 天前
Riverpod 3.0.0 版本中 Provider 类型选择指南
flutter