【问题标题】:Flutter Error: type 'Future<dynamic>' is not a subtype of type 'List<Game>'Flutter 错误:类型“Future<dynamic>”不是“List<Game>”类型的子类型
【发布时间】:2019-04-08 10:14:56
【问题描述】:

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

type 'Future ' 不是 type '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);

});
}

“游戏”是这样的:

factory Game.fromDocument(DocumentSnapshot document) {

return new Game(
  name: document['name'],
  box: document['box'],
  cover: document['cover'],
  description: document['description'],

);
}

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

new HorizontalGameController(getGames()),

知道为什么会发生此错误以及如何解决该错误吗?

编辑:

为了更好地理解这里是我的 Horizo​​ntalGameController:

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

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    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(await getGames()) 更改为下面的代码(用futureBuilder 包装它)。这将使小部件能够使用未来值。

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

    【讨论】:

    • new Horizo​​ntalGameController(getGames()) 给我这个错误:“参数类型 Future > 不能分配给参数类型 List。无论如何感谢您的回复!
    • 查看代码的最后一部分 .. 将该行更改为 new HorizontalGameController(await getGames()) --- 注意 await 关键字
    • 我用 await 尝试过,但 IDE 只是将其标记为“意外的文本等待”。我需要在我的构建小部件异步中设置一些东西吗?
    • 谢谢!现在所有的错误都消失了。但是我的列表似乎是空的......你能告诉我为什么getGames方法中的“print(documents.length”不打印到控制台吗?没有输出。
    • 我不知道。确保产品集合存在并且其中包含数据。确保您的互联网连接正常,然后尝试重新启动/重新安装应用程序
    猜你喜欢
    • 2021-08-13
    • 2022-01-23
    • 2021-10-20
    • 2020-09-20
    • 2021-02-17
    • 2020-01-07
    • 1970-01-01
    • 2021-02-10
    • 2019-06-26
    相关资源
    最近更新 更多