如何在Flutter中更新任何List>。

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

How to update any List<Map<String,dynamic>> in flutter

问题

我刚刚注意到您不希望回答关于翻译的问题,只需要提供翻译的部分。以下是您提供的内容的翻译:

我是Flutter的新手。在更新列表时遇到了一些问题。

目前,我有一个带有一些键和值的列表,如下所示:

[
  {filename: aa}, 
  {filename: bb.txt}, 
  {filename: cc.txt}
] 

我想通过添加一个名为isPresent的参数来更新上述列表,并通过对当前数据进行一些处理来进行更新,如下所示:

[
  {filename: aa,
   isPresent: false}, 
  {filename: bb.txt,
   isPresent: true}, 
  {filename: cc.txt,
  isPresent: false}
] 

如何做到这一点? 

为了实现上述更新的列表,我正在使用以下代码:

final List<Map<String, dynamic>> list = [...fetchJsonData['data']];
list.asMap().forEach((key, value) async {
  Directory? directory;
  directory = await getExternalStorageDirectory();
  File file = File("${directory!.path}/${list[key]['filename']}");
  await io.File(file.path).exists();
  bool isExists = io.File(file.path).existsSync();
  print(isExists);
  list[key]['isPresent'] = isExists;
});
fileList = list;
print(fileList);

但是使用await时似乎不起作用。如果我漏掉了什么,请告诉我。


<details>
<summary>英文:</summary>

I am new to Flutter. I am facing some issues while updating a list. 

Currently, I have a list with some keys and values as follows:

[
{filename: aa},
{filename: bb.txt},
{filename: cc.txt}
]


And I want to update the above list by adding one more parameter called isPresent and update it as follows by doing some process on current data:

[
{filename: aa,
isPresent: false},
{filename: bb.txt,
isPresent: true},
{filename: cc.txt,
isPresent: false}
]


How to do that any idea?

To achieve the above updated list I am using the following code 

final List<Map<String, dynamic>> list = [...fetchJsonData['data']];
list.asMap().forEach((key, value) async {
Directory? directory;
directory = await getExternalStorageDirectory();
File file = File("${directory!.path}/${list[key]['filename']}");
await io.File(file.path).exists();
bool isExists = io.File(file.path).existsSync();
print(isExists);
list[key]['isPresent'] = isExists;
});
fileList = list;
print(fileList);


but does not work with Await I think. Let me know if I am missing something.

</details>


# 答案1
**得分**: 0

```dart
主函数() {
  List<Map<String, dynamic>> 数据 = [
    {'文件名': 'aa'},
    {'文件名': 'bb.txt'},
    {'文件名': 'cc.txt'}
  ];

  数据[0]['是否存在'] = false;
  数据[1]['是否存在'] = true;
  数据[2]['是否存在'] = false;

  打印(数据);
  //[{文件名: aa, 是否存在: false}, {文件名: bb.txt, 是否存在: true}, {文件名: cc.txt, 是否存在: false}]
}
英文:

You can do this for example

main() {
  List&lt;Map&lt;String, dynamic&gt;&gt; data = [
    {&#39;filename&#39;: &#39;aa&#39;},
    {&#39;filename&#39;: &#39;bb.txt&#39;},
    {&#39;filename&#39;: &#39;cc.txt&#39;}
  ];

  data[0][&#39;isPresent&#39;] = false;
  data[1][&#39;isPresent&#39;] = true;
  data[2][&#39;isPresent&#39;] = false;

  print(data);
  //[{filename: aa, isPresent: false}, {filename: bb.txt, isPresent: true}, {filename: cc.txt, isPresent: false}]
}

</details>



# 答案2
**得分**: 0

请尝试以下代码并检查:

List<Map<String, dynamic>> originalList = [
  {"filename": "aa"},
  {"filename": "bb.txt"},
  {"filename": "cc.txt"}
];

List<Map<String, dynamic>> updatedList = originalList.map((item) {
  String filename = item["filename"];
  bool isPresent = /* 根据文件名执行您的处理过程 */;
  
  return {
    "filename": filename,
    "isPresent": isPresent,
  };
}).toList();
英文:

can you try below code and check :

List&lt;Map&lt;String, dynamic&gt;&gt; originalList = [
  {&quot;filename&quot;: &quot;aa&quot;},
  {&quot;filename&quot;: &quot;bb.txt&quot;},
  {&quot;filename&quot;: &quot;cc.txt&quot;}
];

List&lt;Map&lt;String, dynamic&gt;&gt; updatedList = originalList.map((item) {
  String filename = item[&quot;filename&quot;];
  bool isPresent = /* Perform your process here based on the filename */;
  
  return {
    &quot;filename&quot;: filename,
    &quot;isPresent&quot;: isPresent,
  };
}).toList();

答案3

得分: 0

void updateList() {

List<Map<String, dynamic>> data = [
{"filename": "aa"},
{"filename": "bb.txt"},
{"filename": "cc.txt"}
];

for (var file in data) {
file["isPresent"] = file["filename"].endsWith(".txt");
}
print(data);
}

英文:

Sure,

void updateList() {
  
  List&lt;Map&lt;String, dynamic&gt;&gt; data = [
    {&quot;filename&quot;: &quot;aa&quot;},
    {&quot;filename&quot;: &quot;bb.txt&quot;},
    {&quot;filename&quot;: &quot;cc.txt&quot;}
  ];

  for (var file in data) {
    file[&quot;isPresent&quot;] = file[&quot;filename&quot;].endsWith(&quot;.txt&quot;);
  }
  print(data);
}

huangapple
  • 本文由 发表于 2023年6月26日 18:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556006.html
匿名

发表评论

匿名网友

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

确定