将Flutter的可迭代(map)结果添加到List数组中。

huangapple go评论116阅读模式
英文:

Add Flutter Iterable map result into List Array

问题

我们有这个可迭代对象:

  1. Iterable<void> intoarray = listaMap.interests!.map((f) => f['id']);
  2. print(intoarray); <--- 结果:flutter: (1, 3, 4)

我想将结果添加到这个数组中:

  1. List<int?> arrayValues = [];
  2. arrayValues.add(intoarray 中的元素结果)

我应该对其进行整数解析吗?如何将这些结果放入数组中?一次一个还是一次性全部放入?

英文:

We have this iterable:

  1. Iterable&lt;void&gt; intoarray = listaMap.interests!.map((f) =&gt; f[&#39;id&#39;]);
  2. print(intoarray); &lt;--- result: flutter: (1, 3, 4)

And I want add the result into this array:

  1. List&lt;int?&gt; arrayValues = [];
  2. arrayValues.add(intoarray result elements)

Should I parse to int for this? How can I put this results int Array? One by One or everything at once?

答案1

得分: 0

  1. 这是你问题的一个可能解决方案。
  2. void main(){
  3. final List<Map<String, dynamic>> listaMap = [
  4. {
  5. "id":1,
  6. },
  7. {
  8. "id":2,
  9. },
  10. {
  11. "id":3,
  12. }
  13. ];
  14. // 你的代码
  15. //Iterable<void> intoArray = listaMap.map((f) => f['id']);
  16. //print(intoArray);
  17. //解决方案
  18. // // 如果id总是int
  19. // List<int> arrayValues = listaMap.map((f) => f['id'] as int).toList();
  20. // // 如果id可为空
  21. // List<int?> arrayValues = listaMap.map((f) => f['id'] as int?).toList();
  22. // 如果id是String
  23. // List<int?> arrayValues = listaMap.map((f) => int.tryParse(f['id'])).toList();
  24. // print(arrayValues);
  25. // 如果id是动态的
  26. List<int?> arrayValues = listaMap.map((f) {
  27. final id = f['id'];
  28. if (id is int){
  29. return id;
  30. }else if (id is String){
  31. return int.tryParse(id);
  32. }
  33. return null;
  34. }).toList();
  35. print(arrayValues);
  36. }
英文:

Heres a posible solution for your question.

  1. void main(){
  2. final List&lt;Map&lt;String, dynamic&gt;&gt; listaMap = [
  3. {
  4. &quot;id&quot;:1,
  5. },
  6. {
  7. &quot;id&quot;:2,
  8. },
  9. {
  10. &quot;id&quot;:3,
  11. }
  12. ];
  13. // Your code
  14. //Iterable&lt;void&gt; intoArray = listaMap.map((f) =&gt; f[&#39;id&#39;]);
  15. //print(intoArray);
  16. //Solution
  17. // // if id is always int
  18. // List&lt;int&gt; arrayValues = listaMap.map((f) =&gt; f[&#39;id&#39;] as int).toList();
  19. // // if id is nullable
  20. // List&lt;int?&gt; arrayValues = listaMap.map((f) =&gt; f[&#39;id&#39;] as int?).toList();
  21. // if id is String
  22. // List&lt;int?&gt; arrayValues = listaMap.map((f) =&gt; int.tryParse(f[&#39;id&#39;])).toList();
  23. // print(arrayValues);
  24. // if id is dynamic
  25. List&lt;int?&gt; arrayValues = listaMap.map((f) {
  26. final id = f[&#39;id&#39;];
  27. if (id is int){
  28. return id;
  29. }else if (id is String){
  30. return int.tryParse(id);
  31. }
  32. return null;
  33. }).toList();
  34. print(arrayValues);
  35. }

huangapple
  • 本文由 发表于 2023年3月9日 22:53:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定