Home > Net >  How to convert Json inside jsonList into Dart model
How to convert Json inside jsonList into Dart model

Time:06-05

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'])

CodePudding user response:

If you want to serialize nested lists of freezed objects, you must specify:

@JsonSerializable(explicitToJson: true)

or you can also specify so in in your build.yaml file. Note that you must trigger code generation again.
Check the notes at: https://pub.dev/packages/freezed

CodePudding user response:

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;
  }
}

  • Related