简体   繁体   English

Map列表<future<object> &gt; 进入列表<object><div id="text_translate"><p>我有一个</p><pre class="lang-dart prettyprint-override">List&lt;Future&lt;int&gt;&gt;</pre><p> 我如何将其转换为</p><pre class="lang-dart prettyprint-override">List&lt;int&gt;</pre> </div></object></future<object>

[英]Map List<Future<Object>> into List<Object>

I have a我有一个

List<Future<int>> 

how do I transform it into a我如何将其转换为

List<int> 

Credit goes to @pskink归功于@pskink

This is how you can do it:您可以这样做:

  List<int> resultSet = [];

  List<Future<int>> methods = [];

  resultSet = await Future.wait(methods);

You can also do it like this:你也可以这样做:

  List<int> resultSet = [];

  List<Future<int>> methods = [];

  await Future.wait(methods).then((value) {
    resultSet = value;
  });

  print(resultSet);

More about Future/wait更多关于未来/等待

If I have a hint at what you are trying to do, here is my suggestion.如果我对您要执行的操作有提示,这是我的建议。

  • You have a future inside a list, meaning you should await the result of the class itself before adding it to your list.您在列表中有未来,这意味着您应该等待 class 本身的结果,然后再将其添加到列表中。
  • after doing that, you can then do something like this, to convert it to a map:之后,您可以执行以下操作,将其转换为 map:
    var myMap = Map.fromIterable(myList, key: (myClassInstanceVariable) => myClassInstanceVariable.variable1, value: (myClassInstanceVariable) => myClassInstanceVariable.variable1Value);

I would have really loved it, if there was something like a more detailed explanation, so I can see how to help you with the Future.如果有更详细的解释,我会非常喜欢它,这样我就可以看到如何在未来方面为您提供帮助。 for that, I would suggest future builder为此,我建议未来的建设者

FutureBuilder<YourClass>(
    future: theAsyncMethodThatGetsYourClass,
    builder: (context, snapshot){
    //some code
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM