英文:
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<Map<String, dynamic>> data = [
{'filename': 'aa'},
{'filename': 'bb.txt'},
{'filename': 'cc.txt'}
];
data[0]['isPresent'] = false;
data[1]['isPresent'] = true;
data[2]['isPresent'] = 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<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 = /* Perform your process here based on the filename */;
return {
"filename": filename,
"isPresent": 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<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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论