Flutter “颤振”;未处理的异常:类型'_内部链接dhashmap<;字符串,动态>';不是类型为';Iterable<;动态>'&引用;

Flutter “颤振”;未处理的异常:类型'_内部链接dhashmap<;字符串,动态>';不是类型为';Iterable<;动态>'&引用;,flutter,dart,Flutter,Dart,虽然这个问题已经被问了好几次,但我没有从我看到的人那里得到帮助。就这样 我的资产文件夹中有一个模拟json文件:recipe.json 下面是充当我的模型类的recipe.dart: class RecipeModel { final String id; final String name; final String videoLink; final String author; final String category; final String time; R

虽然这个问题已经被问了好几次,但我没有从我看到的人那里得到帮助。就这样

我的资产文件夹中有一个模拟json文件:
recipe.json

下面是充当我的模型类的
recipe.dart

class RecipeModel {
  final String id;
  final String name;
  final String videoLink;
  final String author;
  final String category;
  final String time;
  RecipeModel({
    required this.id,
    required this.name,
    required this.videoLink,
    required this.author,
    required this.category,
    required this.time,
  });

  factory RecipeModel.fromJson(Map<String, dynamic> json) {
    return RecipeModel(
      id: json['id'],
      name: json['name'],
      videoLink: json['videoLink'],
      author: json['author'],
      category: json['category'],
      time: json['time'],
    );
  }
}
如您所见,我希望在页面加载后立即获取数据,并将所述数据保存在列表中,然后可以在
gridview
中使用。但每次加载屏幕时,我都会出现以下错误:

Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>

结果是一个映射而不是一个列表,因为结果是数据,而数据是一个列表,所以首先从映射中提取数据,然后在列表上迭代

Future<List<RecipeModel>> getRecipeData() async {
  // var response = await http.get(
  //   Uri.https("jsonplaceholder.typicode.com", 'users'),
  // );
  String response = await DefaultAssetBundle.of(context)
      .loadString('assets/json/recipe.json');
  var result = json.decode(response);

  var resultList = result["data"];

  List<RecipeModel> recipes = [];
  for (var u in resultList) {
    RecipeModel recipe = RecipeModel(
      id: u['id'] ?? "",
      name: u['name'] ?? "",
      videoLink: u['videoLink'] ?? "",
      author: u['author'] ?? "",
      category: u['category'] ?? "",
      time: u['time'] ?? "",
    );
    recipes.add(recipe);
  }

  print(recipes.length);

  return recipes;
}
Future getRecipeData()异步{
//var response=wait http.get(
//https(“jsonplaceholder.typicode.com”,“users”),
// );
字符串响应=等待DefaultAssetBundle.of(上下文)
.loadString('assets/json/recipe.json');
var result=json.decode(响应);
var resultList=结果[“数据”];
列出食谱=[];
for(结果列表中的变量u){
RecipeModel配方=RecipeModel(
id:u['id']??“,
名称:u['name']??“,
视频链接:u['videoLink']??“,
作者:u[“作者”]??“,
类别:u[“类别”]??“,
时间:u['time']??“,
);
配方。添加(配方);
}
打印(食谱、长度);
返回食谱;
}
如果您对接收的数据类型有任何疑问,只需检查runtimeType


print(result.runtimeType)
结果是一个映射而不是一个列表,因为结果是数据,而数据是一个列表,所以首先从映射中提取数据,然后在列表上迭代

Future<List<RecipeModel>> getRecipeData() async {
  // var response = await http.get(
  //   Uri.https("jsonplaceholder.typicode.com", 'users'),
  // );
  String response = await DefaultAssetBundle.of(context)
      .loadString('assets/json/recipe.json');
  var result = json.decode(response);

  var resultList = result["data"];

  List<RecipeModel> recipes = [];
  for (var u in resultList) {
    RecipeModel recipe = RecipeModel(
      id: u['id'] ?? "",
      name: u['name'] ?? "",
      videoLink: u['videoLink'] ?? "",
      author: u['author'] ?? "",
      category: u['category'] ?? "",
      time: u['time'] ?? "",
    );
    recipes.add(recipe);
  }

  print(recipes.length);

  return recipes;
}
Future getRecipeData()异步{
//var response=wait http.get(
//https(“jsonplaceholder.typicode.com”,“users”),
// );
字符串响应=等待DefaultAssetBundle.of(上下文)
.loadString('assets/json/recipe.json');
var result=json.decode(响应);
var resultList=结果[“数据”];
列出食谱=[];
for(结果列表中的变量u){
RecipeModel配方=RecipeModel(
id:u['id']??“,
名称:u['name']??“,
视频链接:u['videoLink']??“,
作者:u[“作者”]??“,
类别:u[“类别”]??“,
时间:u['time']??“,
);
配方。添加(配方);
}
打印(食谱、长度);
返回食谱;
}
如果您对接收的数据类型有任何疑问,只需检查runtimeType

打印(result.runtimeType)
更改此选项:

for (var u in result)
为此:

for (var u in result["data"])
更改此项:

for (var u in result)
为此:

for (var u in result["data"])

结果是
映射
,而不是
列表
,因为结果是
数据
,而
数据
是列表,所以首先从
映射
提取
数据
,然后在列表上迭代。结果是
映射
,而不是
列表
,因为结果是
数据
,而
数据
是一个列表,因此首先从
映射
中提取
数据
,然后迭代列表。