Search code examples
flutterdartflutter-layoutflutter-testdart-null-safety

How to convert Json inside jsonList into Dart model?


I have been trying to convert JSON to Dart using Code generation. I want to assign List into the messages variable but I'm not able to make this done.

How is it possible to convert the messages into MessagesModel in the user class?

import 'package:freezed_annotation/freezed_annotation.dart';

part 'user.freezed.dart';
part 'user.g.dart';

@freezed
class User with _$User {
  const factory User({
    required int id,
    required String name,
    required String? profilePic,
    required List<Messages>? messages, //error: [Instance of 'Messages', Instance of 'Messages']
  }) = _User;

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}


class Messages {
  Messages({
    required this.isSend,
    required this.time,
    required this.text,
  });
  late final bool isSend;
  late final String time;
  late final String text;
  
  Messages.fromJson(Map<String, dynamic> json){
    isSend = json['is_send'];
    time = json['time'];
    text = json['text'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['is_send'] = isSend;
    _data['time'] = time;
    _data['text'] = text;
    return _data;
  }
}

Sample input:

{
    "id": 0,
    "name": "John Dio",
    "profilePic":
        "https://dummy-profile.png",
    "messages": [
      {
        "isSend": true,
        "time": "12:05 AM",
        "text": "Hi My friend this is a sample text message of type send"
      },
      {
        "isSend": false,
        "time": "12:05 AM",
        "text": "hehe im rplyin to you buddy hehe"
      }
    ]
  }

Log result of the user:

User(id: 0, name: John Dio, profilePic: https://dummy-profile.png, messages: [Instance of 'Messages', Instance of 'Messages'])

Solution

  • Use this in build.yaml

    targets:
      $default:
        builders:
          json_serializable:
            options:
              explicit_to_json: true

    or

    import 'package:freezed_annotation/freezed_annotation.dart';
    
    part 'user.freezed.dart';
    part 'user.g.dart';
    
    @freezed
    class User with _$User {
    JsonSerializable(explicitToJson: true)//specify it here
      const factory User({
        required int id,
        required String name,
        required String? profilePic,
        required List<Messages>? messages, //error: [Instance of 'Messages', Instance of 'Messages']
      }) = _User;
    
      factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
    }
    
    
    class Messages {
      Messages({
        required this.isSend,
        required this.time,
        required this.text,
      });
      late final bool isSend;
      late final String time;
      late final String text;
      
      Messages.fromJson(Map<String, dynamic> json){
        isSend = json['is_send'];
        time = json['time'];
        text = json['text'];
      }
    
      Map<String, dynamic> toJson() {
        final _data = <String, dynamic>{};
        _data['is_send'] = isSend;
        _data['time'] = time;
        _data['text'] = text;
        return _data;
      }
    }