Firebase 颤振错误:类型';未来<;动态>';不是类型为';列表<;游戏>';

Firebase 颤振错误:类型';未来<;动态>';不是类型为';列表<;游戏>';,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我正在尝试建立一个应用程序与不同的游戏列表。作为后端,我使用Firebase,连接工作正常,我对其进行了测试。无论如何,我在用firebase的真实数据替换模拟数据时遇到了问题。我总是会遇到这样的错误: 类型“Future”不是类型“List”的子类型 我有以下功能: getGames() async{ List newGamesList = []; QuerySnapshot result = awaitFirestore.instance.collection('products')

我正在尝试建立一个应用程序与不同的游戏列表。作为后端,我使用Firebase,连接工作正常,我对其进行了测试。无论如何,我在用firebase的真实数据替换模拟数据时遇到了问题。我总是会遇到这样的错误:

类型“Future”不是类型“List”的子类型

我有以下功能:

getGames() async{
 List newGamesList = [];

  QuerySnapshot result = awaitFirestore.instance.collection('products').getDocuments();
  List<DocumentSnapshot> documents = result.documents;
  documents.forEach((DocumentSnapshot doc) {
  Game game = new Game.fromDocument(doc);
  newGamesList.add(game);

});
}
在我的构建小部件中,我称之为“getGames”:

你知道为什么会发生这种错误以及如何解决吗

编辑:

为了更好地理解,这里是我的HorizontalGameController:

class HorizontalGameController extends StatelessWidget {
HorizontalGameController(this.gameItems);
final List<Game> gameItems;

@override
Widget build(BuildContext context) {
return new SizedBox.fromSize(
  size: const Size.fromHeight(240.0),
  child: new ListView.builder(
      itemCount: 1,
      scrollDirection: Axis.horizontal,
      padding: const EdgeInsets.only(left: 12.0, top: 4.0),
      itemBuilder: (BuildContext context, int position) {
        return GameContainerItem(context, gameItems[position]);
      }),
);
}
}
class HorizontalGameController扩展了无状态小部件{
HorizontalGameController(this.gameItems);
最后项目清单;
@凌驾
小部件构建(构建上下文){
返回新的SizedBox.fromSize(
大小:常量大小。从高度(240.0),
子项:新建ListView.builder(
物品计数:1,
滚动方向:轴水平,
填充:仅限常量边集(左:12.0,顶:4.0),
itemBuilder:(构建上下文,int位置){
返回GameContainerItem(上下文,gameItems[位置]);
}),
);
}
}

getGames不会返回您创建的游戏列表。使函数返回游戏列表。我不能测试它,但试试看

Future<List<Game>> getGames() async{
 List<Game> newGamesList = [];

  QuerySnapshot result = await Firestore.instance.collection('products').getDocuments();
  List<DocumentSnapshot> documents = result.documents;
  documents.forEach((DocumentSnapshot doc) {
  Game game = new Game.fromDocument(doc);
  newGamesList.add(game);

});

return newGamesList;
}

//then this
//new HorizontalGameController(await getGames()) //change this 

new HorizontalGameController(getGames())给了我以下错误:“参数类型Future无法分配给参数类型列表。无论如何,感谢您的响应!查看代码的最后部分..将该行更改为
new HorizontalGameController(等待getGames())
---记下
await
关键字我用await尝试了它,但IDE只是将它标记为“意外的文本等待”。我需要在我的构建小部件中设置异步吗?谢谢!所有错误现在都消失了。但是我的列表似乎是空的……你能告诉我为什么“打印(documents.length)”吗“getGames方法内部未打印到控制台?没有输出。我说不出来。请确保产品集合存在并包含其中的数据。确保您的internet连接正常,然后尝试重新启动/重新安装应用程序
class HorizontalGameController extends StatelessWidget {
HorizontalGameController(this.gameItems);
final List<Game> gameItems;

@override
Widget build(BuildContext context) {
return new SizedBox.fromSize(
  size: const Size.fromHeight(240.0),
  child: new ListView.builder(
      itemCount: 1,
      scrollDirection: Axis.horizontal,
      padding: const EdgeInsets.only(left: 12.0, top: 4.0),
      itemBuilder: (BuildContext context, int position) {
        return GameContainerItem(context, gameItems[position]);
      }),
);
}
}
Future<List<Game>> getGames() async{
 List<Game> newGamesList = [];

  QuerySnapshot result = await Firestore.instance.collection('products').getDocuments();
  List<DocumentSnapshot> documents = result.documents;
  documents.forEach((DocumentSnapshot doc) {
  Game game = new Game.fromDocument(doc);
  newGamesList.add(game);

});

return newGamesList;
}

//then this
//new HorizontalGameController(await getGames()) //change this 
FutureBuilder<List<Game>>(
                            future: getGames(),
                            builder: (context, AsyncSnapshot<List<Game>> gamelistSnapshot){
                                  return (gamelistSnapshot.hasData)? HorizontalGameController(gamelistSnapshot.data) : Container();
                            },
                        )