Future.wait() for multiple futures

前端 未结 2 628
遥遥无期
遥遥无期 2020-12-09 13:00

I\'m trying to catch the error when my device has no internet connection. I\'ve built out 2 future methods, 1 to import a json and 1 to look into the database. I have a futu

相关标签:
2条回答
  • 2020-12-09 13:06

    You can use Future.wait to wait for several Future to be completed.

    body: FutureBuilder<List<FlashCardList>>(
        future: Future.wait([
            fetchFlashCardList(),
            fetchFlashCardListFromDB(),
        ]),
    
    0 讨论(0)
  • 2020-12-09 13:10

    Here's an example based on Alexandre's answer (As I found myself looking for how to handle results):

    FutureBuilder(
        future: Future.wait([
             firstFuture(), // Future<bool> firstFuture() async {...}
             secondFuture(),// Future<bool> secondFuture() async {...}
             //... More futures
        ]),
        builder: (
           context, 
           // List of booleans(results of all futures above)
           AsyncSnapshot<List<bool>> snapshot, 
        ){
    
           // Check hasData once for all futures.
           if (!snapshot.hasData) { 
              return CircularProgressIndicator();
           }
    
           // Access first Future's data:
           // snapshot.data[0]
    
           // Access second Future's data:
           // snapshot.data[1]
    
           return Container();
    
        }
    );
    
    
    0 讨论(0)
提交回复
热议问题