skip to Main Content

I got this error when the data field becomes null in the model:

{
  "success": false,
  "data": null,
  "error": "No user found"
}

When this response comes, the code fails. If there is content in the data field, then it works fine.

Below is the code of the model:

// To parse this JSON data, do
//
//     final serviceModel = serviceModelFromJson(jsonString);

import 'dart:convert';

ServiceModel serviceModelFromJson(String str) => ServiceModel.fromJson(json.decode(str));

String serviceModelToJson(ServiceModel data) => json.encode(data.toJson());

class ServiceModel {
    bool success;
    Data? data;
    dynamic error;

    ServiceModel({
        required this.success,
        this.data,
        required this.error,
    });

    factory ServiceModel.fromJson(Map<String, dynamic> json) => ServiceModel(
        success: json["success"],
        data: json["data"] != null ? Data.fromJson(json["data"]) : null,
        error: json["error"],
    );

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

class Data {
    User user;
    String message;

    Data({
        required this.user,
        required this.message,
    });

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

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

class User {
    int userId;
    String name;
    int age;
    String profession;
    String profileImage;

    User({
        required this.userId,
        required this.name,
        required this.age,
        required this.profession,
        required this.profileImage,
    });

    factory User.fromJson(Map<String, dynamic> json) => User(
        userId: json["user_id"],
        name: json["name"],
        age: json["age"],
        profession: json["profession"],
        profileImage: json["profile_image"],
    );

    Map<String, dynamic> toJson() => {
        "user_id": userId,
        "name": name,
        "age": age,
        "profession": profession,
        "profile_image": profileImage,
    };
}

I am passing the JSON response to the model. If there is no error and the data field has content, the model does not fail. But if the data field has no content, the model fails, showing this error:


[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: type 'Null' is not a subtype of type 'Map<String, dynamic>'
#0      new ServiceModel.fromJson (package:untitled/ServiceModel.dart:30:29)
#1      serviceModelFromJson (package:untitled/ServiceModel.dart:13:63)
#2      NetworkService.getresponse (package:untitled/network_utils/network_manager.dart:16:14)
<asynchronous suspension>
#3      _MyHomePageState.buttonPressed (package:untitled/pages/home.dart:49:22)
<asynchronous suspension>

I need to run the code without failure so that I can access the error field in the UI and show the error to the user.

2

Answers


  1. This is not the correct answer but just try this approach

        json["data"] != null && json["data"] is Map<String, dynamic>
      ? Data.fromJson(json["data"])
      : null
    
    Login or Signup to reply.
  2. The issue can be solved by making the Data class field nullable.
    Also add null checks before calling Data.fromJson or User.fromJson.

    class Data {
    User? user;
    String? message;
    
    Data({
        this.user,
        this.message,
    });
    
    factory Data.fromJson(Map<String, dynamic> json) => Data(
        user: json["user"] != null ? User.fromJson(json["user"]) : null,
        message: json["message"],
    );
    
    Map<String, dynamic> toJson() => {
        "user": user?.toJson(),
        "message": message,
    };
    

    }

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