【发布时间】:2022-06-15 20:09:30
【问题描述】:
我有这个功能:
Future<List<String>> _readJson(String path) async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path + '/' + path;
String response = await File(appDocPath).readAsString();
final data = await (json.decode(response) as List<dynamic>).cast<String>();
return data;
}
如您所见,输出为Future<List<String>>。我想将此函数的输出分配给这样的新列表(以便能够遍历元素:
void _function(Map<dynamic, dynamic> playbackData) {
...
List<String> jsonList = readJSON('landmarks.json') as List<String>;
for(int i = 0; i <= jsonList.length - 1; ++i){
print(jsonList[i]);
}
}
但这是我的错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'Future<List<String>>' is not a subtype of type 'List<String>' in type cast
我知道输出是一个未来的字符串列表,但我故意将其转换为一个字符串列表。我哪里错了?
【问题讨论】:
-
不知道为什么你有第二个
await。您是否尝试将您的final data行替换为:final data = (json.decode(response) as List<dynamic>).cast<String>();?