Dart Future.wait for multiple futures and get back results of different types

后端 未结 3 768
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 18:03

I\'m using Flutter to download 3 different sets of data from a server, then do something with all 3 sets. I could do this:

List foos = await downl         


        
3条回答
  •  温柔的废话
    2021-01-18 19:10

    I think is not possible to do in a super nice fashion. All you can do is something like this:

    void main() async {
      List> result = await Future.wait>([
        getStringData(),
        getIntData(),
      ]);
    
      print(result[0]);
      print(result[1]);
    }
    
    Future> getStringData() {
      return Future.value(["a", "b"]);
    }
    
    Future> getIntData() {
      return Future.value([1, 2]);
    }
    

提交回复
热议问题