类型 'Future<dynamic>' 不是类型 'Future<Widget>' 的子类型

type 'Future<dynamic>' is not a subtype of type 'Future<Widget>'

我有一个问题,我试图解决并阅读了很多相关的答案。 但我只是不明白我必须做些什么才能让它发挥作用。

我想显示主题对象列表,它是一个异步函数,因为主题来自 api 调用。 所有这些都是为了创建一个自定义的 gridView,所以我很清楚我必须使用 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());
          },
      )
   ]
 )

调用的函数:

 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,
  );
});

}

很确定我错过了什么,我试图用 'Future List Widget' 替换 'Future Widget' (因为它实际上是一个创建的列表)但是错误是一样的,只是第二部分被替换为'Future List Widget'.

你能帮帮我吗?

所以我有一个开始(不再显示错误)。 我不得不做这样的事情:

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());
        },
      )
    ]
)

还有这个:

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,
  );
 });
}

当然它只显示第一个列表元素,这也不好。还得看看怎么处理。

您可以尝试从构建器中删除 "Widget" 吗?因为它可以将其视为 class 而这可能是您出错的原因

FutureBuilder(
          future: themeColorList.buildCustomGrid(),

或使用动态代替:

FutureBuilder<dynamic>(
          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());
          },
        )),
      ),
    );
  }
}

主要是把GridView放在FutureBuilder里面。 我还重构了 "buildCustomGrid" 方法,而不是 return 小部件,因为它应该是。

希望对您有所帮助!