localJsonToModels<T> static method
定义一个通用的 JSON 数组解析为对象List的方法
使用示例 void exampleUsage() async { List
Implementation
static Future<List<T>> localJsonToModels<T>(
String assetPath, T Function(Map<String, dynamic>) fromJson,
{bool fromSandbox = false}) async {
String response;
if (fromSandbox) {
final file = File(assetPath);
if (!file.existsSync()) {
return [];
}
response = await file.readAsString();
} else {
try {
response = await rootBundle.loadString(assetPath);
} catch (e) {
return [];
}
}
final List<dynamic> data = json.decode(response);
return data.map((item) => fromJson(item)).toList();
}