skip to Main Content
[
  {
    "id": 0,
    "owner": {
      "id": 0,
      "email": "[email protected]",
      "username": "string",
      "phone": "string",
      "is_active": true,
      "is_staff": true,
      "is_premium": true,
      "active_status": 32767,
      "auth_provider": "string",
      "profile": {
        "id": 0,
        "user": "string",
        "last_name": "string",
        "first_name": "string",
        "point": 2147483647,
        "exp": 2147483647,
        "streak": 2147483647,
        "avatar_url": "string",
        "gender": 0,
        "birthdate": "2024-06-11T13:43:43.912Z",
        "height": "-",
        "weight": "04.0",
        "bmi": "93.",
        "level": 0,
        "created_at": "2024-06-11T13:43:43.913Z",
        "updated_at": "2024-06-11T13:43:43.913Z",
        "active_status": 32767
      },
      "following": [
        {
          "id": 0,
          "follower": 0,
          "followed": 0
        }
      ],
      "created_at": "2024-06-11T13:43:43.913Z"
    },
    "title": "string",
    "description": "string",
    "image_url": "string",
    "content": "string",
    "benefit": "string",
    "votes": [
      {
        "id": 0,
        "user": "string",
        "user_id": "string",
        "blog": "string",
        "vote_value": 2147483647
      }
    ],
    "created_at": "2024-06-11T13:43:43.913Z",
    "updated_at": "2024-06-11T13:43:43.913Z",
    "active_status": 2147483647
  }
]

Is the data response I get.

Please help me to map data to use.

2

Answers


  1. You can use quicktype.io and just paste your json and give a class name and it’ll generate your required model class.

    You can play with the available options like:

    • null safety
    • types only
    • generate copyWith method

    Here’s the example of generated code from quicktype.io based on your json

    // To parse this JSON data, do
    //
    //     final userData = userDataFromMap(jsonString);
    
    import 'dart:convert';
    
    List<UserData> userDataFromMap(String str) => List<UserData>.from(json.decode(str).map((x) => UserData.fromMap(x)));
    
    String userDataToMap(List<UserData> data) => json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
    
    class UserData {
        int id;
        Owner owner;
        String title;
        String description;
        String imageUrl;
        String content;
        String benefit;
        List<Vote> votes;
        DateTime createdAt;
        DateTime updatedAt;
        int activeStatus;
    
        UserData({
            required this.id,
            required this.owner,
            required this.title,
            required this.description,
            required this.imageUrl,
            required this.content,
            required this.benefit,
            required this.votes,
            required this.createdAt,
            required this.updatedAt,
            required this.activeStatus,
        });
    
        UserData copyWith({
            int? id,
            Owner? owner,
            String? title,
            String? description,
            String? imageUrl,
            String? content,
            String? benefit,
            List<Vote>? votes,
            DateTime? createdAt,
            DateTime? updatedAt,
            int? activeStatus,
        }) => 
            UserData(
                id: id ?? this.id,
                owner: owner ?? this.owner,
                title: title ?? this.title,
                description: description ?? this.description,
                imageUrl: imageUrl ?? this.imageUrl,
                content: content ?? this.content,
                benefit: benefit ?? this.benefit,
                votes: votes ?? this.votes,
                createdAt: createdAt ?? this.createdAt,
                updatedAt: updatedAt ?? this.updatedAt,
                activeStatus: activeStatus ?? this.activeStatus,
            );
    
        factory UserData.fromMap(Map<String, dynamic> json) => UserData(
            id: json["id"],
            owner: Owner.fromMap(json["owner"]),
            title: json["title"],
            description: json["description"],
            imageUrl: json["image_url"],
            content: json["content"],
            benefit: json["benefit"],
            votes: List<Vote>.from(json["votes"].map((x) => Vote.fromMap(x))),
            createdAt: DateTime.parse(json["created_at"]),
            updatedAt: DateTime.parse(json["updated_at"]),
            activeStatus: json["active_status"],
        );
    
        Map<String, dynamic> toMap() => {
            "id": id,
            "owner": owner.toMap(),
            "title": title,
            "description": description,
            "image_url": imageUrl,
            "content": content,
            "benefit": benefit,
            "votes": List<dynamic>.from(votes.map((x) => x.toMap())),
            "created_at": createdAt.toIso8601String(),
            "updated_at": updatedAt.toIso8601String(),
            "active_status": activeStatus,
        };
    }
    
    class Owner {
        int id;
        String email;
        String username;
        String phone;
        bool isActive;
        bool isStaff;
        bool isPremium;
        int activeStatus;
        String authProvider;
        Profile profile;
        List<Following> following;
        DateTime createdAt;
    
        Owner({
            required this.id,
            required this.email,
            required this.username,
            required this.phone,
            required this.isActive,
            required this.isStaff,
            required this.isPremium,
            required this.activeStatus,
            required this.authProvider,
            required this.profile,
            required this.following,
            required this.createdAt,
        });
    
        Owner copyWith({
            int? id,
            String? email,
            String? username,
            String? phone,
            bool? isActive,
            bool? isStaff,
            bool? isPremium,
            int? activeStatus,
            String? authProvider,
            Profile? profile,
            List<Following>? following,
            DateTime? createdAt,
        }) => 
            Owner(
                id: id ?? this.id,
                email: email ?? this.email,
                username: username ?? this.username,
                phone: phone ?? this.phone,
                isActive: isActive ?? this.isActive,
                isStaff: isStaff ?? this.isStaff,
                isPremium: isPremium ?? this.isPremium,
                activeStatus: activeStatus ?? this.activeStatus,
                authProvider: authProvider ?? this.authProvider,
                profile: profile ?? this.profile,
                following: following ?? this.following,
                createdAt: createdAt ?? this.createdAt,
            );
    
        factory Owner.fromMap(Map<String, dynamic> json) => Owner(
            id: json["id"],
            email: json["email"],
            username: json["username"],
            phone: json["phone"],
            isActive: json["is_active"],
            isStaff: json["is_staff"],
            isPremium: json["is_premium"],
            activeStatus: json["active_status"],
            authProvider: json["auth_provider"],
            profile: Profile.fromMap(json["profile"]),
            following: List<Following>.from(json["following"].map((x) => Following.fromMap(x))),
            createdAt: DateTime.parse(json["created_at"]),
        );
    
        Map<String, dynamic> toMap() => {
            "id": id,
            "email": email,
            "username": username,
            "phone": phone,
            "is_active": isActive,
            "is_staff": isStaff,
            "is_premium": isPremium,
            "active_status": activeStatus,
            "auth_provider": authProvider,
            "profile": profile.toMap(),
            "following": List<dynamic>.from(following.map((x) => x.toMap())),
            "created_at": createdAt.toIso8601String(),
        };
    }
    
    class Following {
        int id;
        int follower;
        int followed;
    
        Following({
            required this.id,
            required this.follower,
            required this.followed,
        });
    
        Following copyWith({
            int? id,
            int? follower,
            int? followed,
        }) => 
            Following(
                id: id ?? this.id,
                follower: follower ?? this.follower,
                followed: followed ?? this.followed,
            );
    
        factory Following.fromMap(Map<String, dynamic> json) => Following(
            id: json["id"],
            follower: json["follower"],
            followed: json["followed"],
        );
    
        Map<String, dynamic> toMap() => {
            "id": id,
            "follower": follower,
            "followed": followed,
        };
    }
    
    class Profile {
        int id;
        String user;
        String lastName;
        String firstName;
        int point;
        int exp;
        int streak;
        String avatarUrl;
        int gender;
        DateTime birthdate;
        String height;
        String weight;
        String bmi;
        int level;
        DateTime createdAt;
        DateTime updatedAt;
        int activeStatus;
    
        Profile({
            required this.id,
            required this.user,
            required this.lastName,
            required this.firstName,
            required this.point,
            required this.exp,
            required this.streak,
            required this.avatarUrl,
            required this.gender,
            required this.birthdate,
            required this.height,
            required this.weight,
            required this.bmi,
            required this.level,
            required this.createdAt,
            required this.updatedAt,
            required this.activeStatus,
        });
    
        Profile copyWith({
            int? id,
            String? user,
            String? lastName,
            String? firstName,
            int? point,
            int? exp,
            int? streak,
            String? avatarUrl,
            int? gender,
            DateTime? birthdate,
            String? height,
            String? weight,
            String? bmi,
            int? level,
            DateTime? createdAt,
            DateTime? updatedAt,
            int? activeStatus,
        }) => 
            Profile(
                id: id ?? this.id,
                user: user ?? this.user,
                lastName: lastName ?? this.lastName,
                firstName: firstName ?? this.firstName,
                point: point ?? this.point,
                exp: exp ?? this.exp,
                streak: streak ?? this.streak,
                avatarUrl: avatarUrl ?? this.avatarUrl,
                gender: gender ?? this.gender,
                birthdate: birthdate ?? this.birthdate,
                height: height ?? this.height,
                weight: weight ?? this.weight,
                bmi: bmi ?? this.bmi,
                level: level ?? this.level,
                createdAt: createdAt ?? this.createdAt,
                updatedAt: updatedAt ?? this.updatedAt,
                activeStatus: activeStatus ?? this.activeStatus,
            );
    
        factory Profile.fromMap(Map<String, dynamic> json) => Profile(
            id: json["id"],
            user: json["user"],
            lastName: json["last_name"],
            firstName: json["first_name"],
            point: json["point"],
            exp: json["exp"],
            streak: json["streak"],
            avatarUrl: json["avatar_url"],
            gender: json["gender"],
            birthdate: DateTime.parse(json["birthdate"]),
            height: json["height"],
            weight: json["weight"],
            bmi: json["bmi"],
            level: json["level"],
            createdAt: DateTime.parse(json["created_at"]),
            updatedAt: DateTime.parse(json["updated_at"]),
            activeStatus: json["active_status"],
        );
    
        Map<String, dynamic> toMap() => {
            "id": id,
            "user": user,
            "last_name": lastName,
            "first_name": firstName,
            "point": point,
            "exp": exp,
            "streak": streak,
            "avatar_url": avatarUrl,
            "gender": gender,
            "birthdate": birthdate.toIso8601String(),
            "height": height,
            "weight": weight,
            "bmi": bmi,
            "level": level,
            "created_at": createdAt.toIso8601String(),
            "updated_at": updatedAt.toIso8601String(),
            "active_status": activeStatus,
        };
    }
    
    class Vote {
        int id;
        String user;
        String userId;
        String blog;
        int voteValue;
    
        Vote({
            required this.id,
            required this.user,
            required this.userId,
            required this.blog,
            required this.voteValue,
        });
    
        Vote copyWith({
            int? id,
            String? user,
            String? userId,
            String? blog,
            int? voteValue,
        }) => 
            Vote(
                id: id ?? this.id,
                user: user ?? this.user,
                userId: userId ?? this.userId,
                blog: blog ?? this.blog,
                voteValue: voteValue ?? this.voteValue,
            );
    
        factory Vote.fromMap(Map<String, dynamic> json) => Vote(
            id: json["id"],
            user: json["user"],
            userId: json["user_id"],
            blog: json["blog"],
            voteValue: json["vote_value"],
        );
    
        Map<String, dynamic> toMap() => {
            "id": id,
            "user": user,
            "user_id": userId,
            "blog": blog,
            "vote_value": voteValue,
        };
    }
    
    

    So you can just paste those generated models in your code in a dart file.
    And when you need to map the response you can just use something like this:

    final model = ModelName.fromMap(yourJsonResponseString);
    
    Login or Signup to reply.
  2. Actually the data that you provided is a List<Map<String,dynamic>> so the best way to parse this type of data is to use packages like json_serializable and build_runner for generating the code for the model of the data or either try to understand the data .if you see data stating with [ and ending with ] this is an list(Array) and if its start with { and ends with } it will be a map(object) so by this you can understand the structure of data you can use json.decode() for parsing it to dart data type if its json.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search