颤振获取json嵌套数据

时间:2021-04-30 20:00:21

标签: flutter dart

这是我得到的json数据:

{
    "data":
    [
        {
            "id": "1",
            "name": "Kacchi Biriyani",
            "videoLink": "https://www.youtube.com/watch?v=K4TOrB7at0Y",
            "author": "Alan Ford",
            "category":"Biriyani",
            "time": "15 min",
            "steps": {
                "step 1": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                "step 2": "Suspendisse vel sapien elit"
            },
        },
        {
            "id": "2",
            "name": "Mughal Biriyani",
            "videoLink": "https://www.youtube.com/watch?v=aNVviTECNM0",
            "author": "Ricky James",
            "category":"Biriyani",
            "time": "10 min",
            "steps": {
                "step 1": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                "step 2": "Suspendisse vel sapien elit",
                "step 3": "Proin luctus, quam non dapibus pretium"
            },
        },
   ]
}

如您所见,这些步骤的长度可以是可变的。对于某些食谱,它可能需要 10 个步骤,对于某些食谱,它可能只需要 2 个步骤。

这是我实现的模型。 recipe.model.dart

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

  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'],
      steps: json['steps'],
    );
  }
}

这是我使用 tp 获取数据的方法:

  List<RecipeModel> recipes = [];
  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);

    for (var u in result["data"]) {
      RecipeModel recipe = RecipeModel(
        id: u['id'] ?? "",
        name: u['name'] ?? "",
        videoLink: u['videoLink'] ?? "",
        author: u['author'] ?? "",
        category: u['category'] ?? "",
        time: u['time'] ?? "",
        steps: u['steps'] ?? "", -----------------> need to store the steps for each recipe
      );
      recipes.add(recipe);
    }
    return recipes;
  }

现在我将把这些步骤发送到另一个页面。我正在做的是:

RecipeCard(
  itemName: recipes[index].name,
  categoryName: recipes[index].category,
  time: recipes[index].time,
  videoLink: recipes[index].videoLink,
  authorName: recipes[index].author,
  //need to send the steps for selected recipe
);

我在这里遗漏了什么?

1 个答案:

答案 0 :(得分:1)

您对步骤的定义是 List<String> 但您传递的是 Map<String, String>

试试这个:

steps: u['steps'] != null ? List<String>.from(u['steps'].values) : [],