type "null" is not a subtype of type 'List<dynamic>' in type cast- nested json

41 Views Asked by At

i want to retrive or parsing nested JSON data from rapidapi : the recipe class should include basic data name,image and contain object of rating{score} and list of instruction{ position, text} the structure of data in the image below

JSON data in rapid api displayed in the image

user rating in JSON Instruction list in JSON

Recipe class

class Recipe {
  final String image;
  final rating Rating;
  final int? time;
  final int? personNum;
  final String name;
  final String description;
  final List<instruction> instructions;
  //final int? instructions;

  Recipe({required this.Rating,required this.instructions,required this.personNum,required this.time,required this.name,required this.image, required this.description});

  factory Recipe.fromJson(Map<String, dynamic> json) {

    var list=json['instructions'] as List;
    print(list.runtimeType);
    List<instruction> instructionList=list.map((i) => instruction.fromJson(i)).toList();

    return Recipe(
      image: json['thumbnail_url'],
      Rating: rating.fromJson(json['user_ratings']),
      time:json['total_time_minutes'],
      name: json['name'],
      description: json['description'],
      instructions: instructionList,
      personNum: json['num_servings'],
    );
  }
}

rating class

class rating{
  int score;

  rating({required this.score});

  factory rating.fromJson(Map<String, dynamic> json) {

    return rating(
      score: json['score'],

    );
  }

instruction class

class instruction {
  final int stepNum;
  final String text;

  instruction({ required this.text,required this.stepNum});

  factory instruction.fromJson(Map<String, dynamic> json) {
    return instruction(
      text: json['display_text'],
      stepNum: json['position'],
    );
  }
0

There are 0 best solutions below