1

here is the actual api response in postman { "status": 200, "message": "", "data": { "userDetails": { "username": "richu", "email": "test96@gmail.com", "id": "1" }, "posts": [ { "id": "1", "user_id": "1", "post": "post 1 -- hello", "imagepath": "uploads/posts/1.png", "post_date": "2020-11-07 09:10:07", "status": "0" }, { "id": "2", "user_id": "1", "post": "post 2-- hello", "imagepath": "uploads/posts/2.png", "post_date": "2020-11-07 10:10:07", "status": "0" }, { "id": "3", "user_id": "1", "post": "post 3-- sfdsfsdfsdfsdfsdfsdfsdfvbcvb", "imagepath": "uploads/posts/3.png", "post_date": "2020-11-07 11:10:07", "status": "0" } ], "followers": "5", "following": "0" } } programme am working out I need a list of posts but it returns only {"status":200,"message":"","data":{"userDetails":{"username":"Admin","email":"test96@gmail.com","id":"1"},"posts":null,"followers":"9","following":"0"}} shows error that E/flutter (12419): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'map' was called on null. E/flutter (12419): Receiver: null E/flutter (12419): Tried calling: map(Closure: (dynamic) => Posts) E/flutter (12419): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) E/flutter (12419): #1 new Data.fromJson (package:campgain_mobile/src/models/api_models/user_profile_response.dart:55:47) well my code is as follows

 `
import 'dart:convert';

import 'package:flutter/foundation.dart';

UserProfileResponse userProfileResponseFromJson(String str) =>
    UserProfileResponse.fromJson(json.decode(str));

String userProfileResponseToJson(UserProfileResponse data) =>
    json.encode(data.toJson());

class UserProfileResponse {
  UserProfileResponse({
    this.status,
    this.message,
    this.data,
  });

  final int status;
  final String message;
  final Data data;

  factory UserProfileResponse.fromJson(Map<String, dynamic> json) =>
      UserProfileResponse(
        status: json["status"],
        message: json["message"],
        data: Data.fromJson(json["data"]),
      );

  Map<String, dynamic> toJson() => {
        "status": status,
        "message": message,
        "data": data.toJson(),
      };
}

class Data {
  Data({
    this.userDetails,
    this.posts,
    this.followers,
    this.following,
  });

  final UserDetails userDetails;
  final List<Posts> posts;
  final String followers;
  final String following;

  factory Data.fromJson(Map<String, dynamic> json) => Data(
        userDetails: UserDetails.fromJson(json["userDetails"]),
        posts: List<Posts>.from(json["posts"].map((x) => Posts.fromJson(x)))
            ,
        followers: json["followers"],
        following: json["following"],
      );

  Map<String, dynamic> toJson() => {
        "userDetails": userDetails.toJson(),
        "posts": List<dynamic>.from(posts.map((x) => x.toJson())),
        "followers": followers,
        "following": following,
      };
}

class Posts {
  Posts({
    this.id,
    this.userId,
    this.post,
    this.imagepath,
    this.postDate,
    this.status,
  });

  final String id;
  final String userId;
  final String post;
  final String imagepath;
  final DateTime postDate;
  final String status;

  factory Posts.fromJson(Map<String, dynamic> json) => Posts(
        id: json["id"],
        userId: json["user_id"],
        post: json["post"],
        imagepath: json["imagepath"],
        postDate: DateTime.parse(json["post_date"]),
        status: json["status"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "user_id": userId,
        "post": post,
        "imagepath": imagepath,
        "post_date": postDate.toIso8601String(),
        "status": status,
      };
}

class UserDetails {
  UserDetails({
    this.username,
    this.email,
    this.id,
  });

  String username;
  String email;
  String id;

  factory UserDetails.fromJson(Map<String, dynamic> json) => UserDetails(
        username: json["username"],
        email: json["email"],
        id: json["id"],
      );

  Map<String, dynamic> toJson() => {
        "username": username,
        "email": email,
        "id": id,
      };
}
` 
A.K.J.94
  • 492
  • 6
  • 14

2 Answers2

0

You need to add null safety to JSON keys, try this:

class UserProfileResponse {
  int status;
  String message;
  Data data;

  UserProfileResponse({this.status, this.message, this.data});

  UserProfileResponse.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    message = json['message'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['status'] = this.status;
    data['message'] = this.message;
    if (this.data != null) {
      data['data'] = this.data.toJson();
    }
    return data;
  }
}

class Data {
  UserDetails userDetails;
  List<Posts> posts;
  String followers;
  String following;

  Data({this.userDetails, this.posts, this.followers, this.following});

  Data.fromJson(Map<String, dynamic> json) {
    userDetails = json['userDetails'] != null
        ? new UserDetails.fromJson(json['userDetails'])
        : null;
    if (json['posts'] != null) {
      posts = new List<Posts>();
      json['posts'].forEach((v) {
        posts.add(new Posts.fromJson(v));
      });
    }
    followers = json['followers'];
    following = json['following'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.userDetails != null) {
      data['userDetails'] = this.userDetails.toJson();
    }
    if (this.posts != null) {
      data['posts'] = this.posts.map((v) => v.toJson()).toList();
    }
    data['followers'] = this.followers;
    data['following'] = this.following;
    return data;
  }
}

class UserDetails {
  String username;
  String email;
  String id;

  UserDetails({this.username, this.email, this.id});

  UserDetails.fromJson(Map<String, dynamic> json) {
    username = json['username'];
    email = json['email'];
    id = json['id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['username'] = this.username;
    data['email'] = this.email;
    data['id'] = this.id;
    return data;
  }
}

class Posts {
  String id;
  String userId;
  String post;
  String imagepath;
  String postDate;
  String status;

  Posts(
      {this.id,
      this.userId,
      this.post,
      this.imagepath,
      this.postDate,
      this.status});

  Posts.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['user_id'];
    post = json['post'];
    imagepath = json['imagepath'];
    postDate = json['post_date'];
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['user_id'] = this.userId;
    data['post'] = this.post;
    data['imagepath'] = this.imagepath;
    data['post_date'] = this.postDate;
    data['status'] = this.status;
    return data;
  }
}
Ishanga Vidusha
  • 284
  • 3
  • 9
0

The error occurs when you try to call map method on json['posts']. if json['posts'] is null then null pointer exception occurs so you need to make sure that your encoders and decoders are null safe. use the code below

import 'dart:convert';

UserProfileResponse userProfileResponseFromJson(String str) =>
    UserProfileResponse.fromJson(json.decode(str));

String userProfileResponseToJson(UserProfileResponse data) =>
    json.encode(data.toJson());

class UserProfileResponse {
  UserProfileResponse({
    this.status,
    this.message,
    this.data,
  });

  final int status;
  final String message;
  final Data data;

  factory UserProfileResponse.fromJson(Map<String, dynamic> json) =>
      UserProfileResponse(
        status: json["status"],
        message: json["message"],
        data: Data.fromJson(json["data"]),
      );

  Map<String, dynamic> toJson() => {
        "status": status,
        "message": message,
        "data": data?.toJson(),
      };
}

class Data {
  Data({
    this.userDetails,
    this.posts,
    this.followers,
    this.following,
  });

  final UserDetails userDetails;
  final List<Posts> posts;
  final String followers;
  final String following;

  factory Data.fromJson(Map<String, dynamic> json) {
    return json != null
        ? Data(
            userDetails: UserDetails.fromJson(json["userDetails"]),
            posts: json["posts"] != null
                ? List<Posts>.from(json["posts"].map((x) => Posts.fromJson(x)))
                : null,
            followers: json["followers"],
            following: json["following"],
          )
        : null;
  }

  Map<String, dynamic> toJson() => {
        "userDetails": userDetails?.toJson(),
        "posts": posts != null
            ? List<dynamic>.from(posts?.map((x) => x.toJson()))
            : null,
        "followers": followers,
        "following": following,
      };
}

class Posts {
  Posts({
    this.id,
    this.userId,
    this.post,
    this.imagepath,
    this.postDate,
    this.status,
  });

  final String id;
  final String userId;
  final String post;
  final String imagepath;
  final DateTime postDate;
  final String status;

  factory Posts.fromJson(Map<String, dynamic> json) {
    return json != null
        ? Posts(
            id: json["id"],
            userId: json["user_id"],
            post: json["post"],
            imagepath: json["imagepath"],
            postDate: DateTime.parse(json["post_date"]),
            status: json["status"],
          )
        : null;
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "user_id": userId,
        "post": post,
        "imagepath": imagepath,
        "post_date": postDate?.toIso8601String(),
        "status": status,
      };
}

class UserDetails {
  UserDetails({
    this.username,
    this.email,
    this.id,
  });

  String username;
  String email;
  String id;

  factory UserDetails.fromJson(Map<String, dynamic> json) {
    return json != null
        ? UserDetails(
            username: json["username"],
            email: json["email"],
            id: json["id"],
          )
        : null;
  }

  Map<String, dynamic> toJson() => {
        "username": username,
        "email": email,
        "id": id,
      };
}
Benyam S
  • 181
  • 3