Future.wait() for multiple futures

前端 未结 2 630
遥遥无期
遥遥无期 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: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 firstFuture() async {...}
             secondFuture(),// Future secondFuture() async {...}
             //... More futures
        ]),
        builder: (
           context, 
           // List of booleans(results of all futures above)
           AsyncSnapshot> 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();
    
        }
    );
    
    

提交回复
热议问题