skip to Main Content
I have a JSON like below comes from the server side:

{
  movie:
  {
    title : String?,
    description: String?,
    id: String?,
    image: String?,
    videos : 
    [
      video[i]: 
      {
        trailers:
        [
          trailers[i]: 
          {
            title:String?
            option1: String?,
            option2: String?,
            option3: String?,
            availables: List<bool>?,
          },
        ]
      },
    ]
  }
}

I am trying to create a models/movie class that can receive and handle such a JSON variable. What I have done so far is the easy part:

class Movie with ChangeNotifier {
  String id;
  String? title;
  String? image;
  List<Map<String,dynamic>>? videos;
}

I don’t know how to continue and define the sub variables of the videos?

EDIT I don’t have access to the real data at the moment but the JSON would be like the following I think:

{
  "movie":
  {
    "title" : "String",
    "description": "String",
    "id": "String",
    "image": "String",
    "videos" : 
    [
      "video": 
      {
        "trailers":
        [
          "trailers": 
          {
            "title":"String",
            "option1": "String",
            "option2": "String",
            "option3": "String",
             "availables": 
             {
                 "first": true,
                 "second": true,
                 "third": true,
            },         
          },
        ],
      },
    ],
  },
}

2

Answers


  1. May be you need this: Json to Dart Converter.

    Try this website this is help you create model class according to your json response.

    https://quicktype.io/

    If any issue comment me i will provide you solutions.

    Login or Signup to reply.
  2. The json you given is not valid json you can check it on json-validator online
    i changed the json and make it valid here is the valid version

    {
    "movie": {
        "title": "String",
        "description": "String",
        "id": "String",
        "image": "String",
        "videos": [
            {
                "video": {
                    "trailers": [
                        {
                            "trailers": {
                                "title": "String",
                                "option1": "String",
                                "option2": "String",
                                "option3": "String",
                                "availables": {
                                    "first": true,
                                    "second": true,
                                    "third": true
                                }
                            }
                        }
                    ]
                }
            }
        ]
     }
    }
    

    and the model for this is given as which i created using json to dart website

        class Movie {
      String? title;
      String? description;
      String? id;
      String? image;
      List<Videos>? videos;
    
      Movie({this.title, this.description, this.id, this.image, this.videos});
    
      Movie.fromJson(Map<String, dynamic> json) {
        title = json['title'];
        description = json['description'];
        id = json['id'];
        image = json['image'];
        if (json['videos'] != null) {
          videos = <Videos>[];
          json['videos'].forEach((v) {
            videos!.add(Videos.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = <String, dynamic>{};
        data['title'] = title;
        data['description'] = description;
        data['id'] = id;
        data['image'] = image;
        if (videos != null) {
          data['videos'] = videos!.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class Videos {
      Video? video;
    
      Videos({this.video});
    
      Videos.fromJson(Map<String, dynamic> json) {
        video = json['video'] != null ? Video.fromJson(json['video']) : null;
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = {};
        if (video != null) {
          data['video'] = video!.toJson();
        }
        return data;
      }
    }
    
    class Video {
      List<Trailers>? trailers;
    
      Video({this.trailers});
    
      Video.fromJson(Map<String, dynamic> json) {
        if (json['trailers'] != null) {
          trailers = <Trailers>[];
          json['trailers'].forEach((v) {
            trailers!.add(Trailers.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = {};
        if (trailers != null) {
          data['trailers'] = trailers!.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class Trailers {
      String? title;
      String? option1;
      String? option2;
      String? option3;
      Availables? availables;
    
      Trailers(
          {this.title, this.option1, this.option2, this.option3, this.availables});
    
      Trailers.fromJson(Map<String, dynamic> json) {
        title = json['title'];
        option1 = json['option1'];
        option2 = json['option2'];
        option3 = json['option3'];
        availables = json['availables'] != null
            ? Availables.fromJson(json['availables'])
            : null;
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = {};
        data['title'] = title;
        data['option1'] = option1;
        data['option2'] = option2;
        data['option3'] = option3;
        if (availables != null) {
          data['availables'] = availables!.toJson();
        }
        return data;
      }
    }
    
    class Availables {
      bool? first;
      bool? second;
      bool? third;
    
      Availables({this.first, this.second, this.third});
    
      Availables.fromJson(Map<String, dynamic> json) {
        first = json['first'];
        second = json['second'];
        third = json['third'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = {};
        data['first'] = first;
        data['second'] = second;
        data['third'] = third;
        return data;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search