skip to Main Content
{
    "status": "success",
    "errors": null,
    "message": "Records Found successfully.",
    "data": [
        {
            "id": 2,
            "department_id": 1,
            "permissions": "{"eventStatus":"1","dptStatus":"1","empStatus":"1","policyStatus":"1","openingStatus":"1","birthdayStatus":"1","hrdStatus":"1","supportStatus":"1","libraryStatus":"1","exemptionStatus":"1","settingStatus":"1","assetsStatus":"1"}"
        }
    ]
}

How to convert this json to dart model class?

2

Answers


  1. You either do it on your own, with jsonDecode() and Map, or use the json_serializable package.

    Using jsonDecode()

    1. deserialize the json with jsonDecode: final jsonMap = jsonDecode(jsonString) as Map<String, dynamic>;
    2. create the class with a constructor that takes in the map
    class ResponseModel {
      final String status;
      // Don't know what this looks like
      final Object errors;
      final String message;
      final DataModel data;
    
      ResponseModel(this.status, this.errors);
    
      ResponseModel.fromJson(Map<String, dynamic> json)
          : status = json['status'] as String,
            errors = json['errors'] as Object,
            message = json['message'] as String,
            data = DataModel.fromJson(json['data']);
    
      // optionally a toJson() as well
      Map<String, dynamic> toJson() => {
        'status': status,
        'errors': errors,
        'message': message,
        'data': data.toJson()
      };
    }
    
    class DataModel {
      final String id;
      final int department_id;
      final String permissions;
    
      DataModel(this.status, this.errors);
    
      DataModel.fromJson(Map<String, dynamic> json)
          : id = json['id'] as String,
            department_id = json['department_id'] as int,
            permissions = json['permissions'] as String
    
      // optionally a toJson() as well
      Map<String, dynamic> toJson() => {
        'id': id,
        'department_id': department_id,
        'permissions': permissions
      };
    }
    

    Using the json_serializable package

    1. add the dependencies with: flutter pub add json_annotation dev:build_runner dev:json_serializable
    2. create the class
    class ResponseModel {
      final String status;
      final Object errors;
      // ... rest of the fields
    
      ResponseModel({required this.status, required this.errors});
    
      // these two methods will show an error, they will be generated later
      factory ResponseModel.fromJson(Map<String, dynamic> json) => _$ResponseModelFromJson(json);
    
      Map<String, dynamic> toJson() => _$ResponseModelToJson(this);
    }
    
    1. run build_runner: dart run build_runner build
    Login or Signup to reply.
  2. You can use this library to decode:

    import 'dart:convert';
    

    It has a method

    json.decode()
    

    This will decode the josnData.

    You can also explore this website to make a model class.

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