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

文章讲述了在Flutter中遇到的类型转换错误,即Future<int>不能直接转换为int。作者提供了问题分析,指出应使用.then方法处理DatabaseHelper的异步调用,并给出了修改后的代码示例。
摘要由CSDN通过智能技术生成

在这里插入图片描述

问题描述

今天有个同事调试flutter程序时报错,问我怎么解决,程序运行时报如下错误:
type ‘Future’ is not a subtype of type ‘int’ in type cast

image.png

错误源码

                            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的精彩世界!
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值