Flutter 类型';未来<;动态>';不是类型为';未来<;Widget>';
我有一个问题,我试图解决,并阅读了大量的答案有关它。 但我不明白我该怎么做才能让它工作 我想显示一个主题对象列表,它是一个异步函数,因为主题来自api调用。 所有这些都是为了创建自定义gridView,所以我很清楚我必须使用FutureBuilder,但即使在使用它时,我也会收到以下错误消息: 我的代码如下所示:Flutter 类型';未来<;动态>';不是类型为';未来<;Widget>';,flutter,widget,Flutter,Widget,我有一个问题,我试图解决,并阅读了大量的答案有关它。 但我不明白我该怎么做才能让它工作 我想显示一个主题对象列表,它是一个异步函数,因为主题来自api调用。 所有这些都是为了创建自定义gridView,所以我很清楚我必须使用FutureBuilder,但即使在使用它时,我也会收到以下错误消息: 我的代码如下所示: GridView.count( crossAxisCount: 2, children: <Widget>[ FutureBuilder<
GridView.count(
crossAxisCount: 2,
children: <Widget>[
FutureBuilder<Widget>(
future: themeColorList.buildCustomGrid(),
builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
if(snapshot.hasData)
print("snapshot.data : ${snapshot.data}");
return snapshot.data;
return Container(child: CircularProgressIndicator());
},
)
]
)
GridView.count(
交叉轴计数:2,
儿童:[
未来建设者(
future:themeColorList.buildCustomGrid(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasData)
打印(“snapshot.data:${snapshot.data}”);
返回快照数据;
返回容器(子项:CircularProgressIndicator());
},
)
]
)
函数名为:
buildCustomGrid() async {
List<ProjectTheme> themeList = await constructTheme();
List.generate(themes.length, (index) {
return Container(
child: Center(
child: Text(themeList[index].themeName),
),
color: themeList[index].themeColor,
);
});
buildCustomGrid()异步{
List themeList=Wait ConstructionTheme();
List.generate(themes.length,(index){
返回容器(
儿童:中心(
子项:文本(主题列表[index].themeName),
),
颜色:主题列表[索引]。主题颜色,
);
});
}
我肯定错过了一些东西,我尝试用“未来列表小部件”替换“未来小部件”(因为它实际上是一个创建的列表),但错误是相同的,只是第二部分被“未来列表小部件”替换
你能帮帮我吗
所以我有了一个开始(不再显示错误)。
我必须做这样的事情:
GridView.count(
crossAxisCount: 2,
children: <Widget>[
FutureBuilder<List<Widget>>(
future: themeColorList.buildCustomGrid(),
builder: (BuildContext context, AsyncSnapshot<List<Widget>> snapshot){
if(snapshot.hasData) {
for(int i = 0; i< snapshot.data.length; i++) {
print("snapshot.data : ${snapshot.data[i]}");
return snapshot.data[i];
}
}
return Container(child: CircularProgressIndicator());
},
)
]
)
GridView.count(
交叉轴计数:2,
儿童:[
未来建设者(
future:themeColorList.buildCustomGrid(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasData){
对于(int i=0;i
这是:
Future<List<Widget>> buildCustomGrid() async {
List<ProjectTheme> themeList = await constructTheme();
return List.generate(themes.length, (index) {
return Container(
child: Center(
child: Text(themeList[index].themeName),
),
color: themeList[index].themeColor,
);
});
}
Future buildCustomGrid()异步{
List themeList=Wait ConstructionTheme();
返回列表.生成(themes.length,(索引){
返回容器(
儿童:中心(
子项:文本(主题列表[index].themeName),
),
颜色:主题列表[索引]。主题颜色,
);
});
}
当然,它只显示第一个列表元素,这也不好。你必须看看如何处理它。你能试着从你的构建器中删除“Widget”吗?因为它可以将其视为类,这可能是导致错误的原因
FutureBuilder(
future: themeColorList.buildCustomGrid(),
或使用动态,而不是:
FutureBuilder<dynamic>(
future: themeColorList.buildCustomGrid(),
FutureBuilder(
future:themeColorList.buildCustomGrid(),
我为您制作了完整的工作代码:
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class ProjectTheme {
String themeName;
Color themeColor;
ProjectTheme({this.themeName, this.themeColor});
}
class MyApp extends StatelessWidget {
Future<List<ProjectTheme>> getThemes() async {
List<ProjectTheme> themeList = [
ProjectTheme(themeName: "red", themeColor: Colors.red),
ProjectTheme(themeName: "green", themeColor: Colors.green),
ProjectTheme(themeName: "blue", themeColor: Colors.blue),
];
return themeList;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: FutureBuilder<List<ProjectTheme>>(
future: getThemes(),
builder: (BuildContext context,
AsyncSnapshot<List<ProjectTheme>> snapshot) {
if (snapshot.hasData)
return GridView.count(
crossAxisCount: 2,
children: snapshot.data
.map(
(theme) => Container(
child: Center(
child: Text(theme.themeName),
),
color: theme.themeColor,
),
)
.toList(),
);
return Container(child: CircularProgressIndicator());
},
)),
),
);
}
}
导入“包装:颤振/材料.省道”;
最终颜色深蓝色=颜色。来自argb(255,18,32,47);
void main(){
runApp(MyApp());
}
课堂专题{
字符串themeName;
颜色-颜色;
项目主题({this.themeName,this.themeColor});
}
类MyApp扩展了无状态小部件{
Future getThemes()异步{
列出主题列表=[
项目主题(主题名称:“红色”,主题颜色:Colors.red),
项目主题(主题名称:“绿色”,主题颜色:颜色。绿色),
项目主题(主题名称:“蓝色”,主题颜色:Colors.blue),
];
返回主题列表;
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
主题:ThemeData.dark().copyWith(脚手架背景颜色:深蓝色),
debugShowCheckedModeBanner:false,
家:脚手架(
正文:中(
孩子:未来建设者(
future:getThemes(),
生成器:(BuildContext上下文,
异步快照(快照){
if(snapshot.hasData)
返回GridView.count(
交叉轴计数:2,
子项:snapshot.data
.地图(
(主题)=>容器(
儿童:中心(
子:文本(theme.themeName),
),
颜色:theme.themeColor,
),
)
.toList(),
);
返回容器(子项:CircularProgressIndicator());
},
)),
),
);
}
}
主要是将GridView放在FutureBuilder中。
我还对“buildCustomGrid”方法进行了重构,使其不返回应该返回的小部件
希望它能有所帮助!如果您尝试在需要小部件时提供一个列表,这是一个问题。请尝试在buildCustomGrid()上声明返回类型,你的IDE应该会给你一些关于数据类型的提示。我用修正来编辑我的文章。哇,非常感谢,它的工作原理是这样的。感谢你花时间制作它,感谢大家的帮助。很棒的社区。