flutterdartjsonserializerdynamictype

In Dart, how do I serialize classes to and from JSON without using `dynamic` and without using code generation? Like Netwonsoft for C#?


I have a class like this:

class UserModel {
  String id = '';
  String firstName = '';
  String lastName = '';
  String email = '';
  String phoneNumber = '';
  String? createdDate = '';
  String? membershipType = '';

  UserModel({
    required this.id,
    required this.firstName,
    required this.lastName,
    required this.email,
    required this.phoneNumber,
    required this.createdDate,
    required this.membershipType
  });

  UserModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    firstName = json['firstName'];
    lastName = json['lastName'];
    email = json['email'];
    phoneNumber = json['phoneNumber'];
    createdDate = json['createdDate'];
    membershipType = json['membershipType'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['firstName'] = firstName;
    data['lastName'] = lastName;
    data['email'] = email;
    data['phoneNumber'] = phoneNumber;
    data['createdDate'] = createdDate;
    data['membershipType'] = membershipType;
    return data;
  }
}

I want to avoid using dynamic type and I want to cleanly handle errors that may occur from malformed JSON. I also want to avoid code generation since it will mess up my build pipelines and introduce significant overhead into my project.

Is there a library that uses generics or other features to provide a better developer experience? Is there a better way to manually implement JSON serialization without dynamic?


Solution

  • In dart language, JSON used dynamic because we cannot expect one data type because we have different data types such as String , Int , List like that.

    But in this issue, we want to avoid dynamic so , we can use exact data type with nullsefty instance of dynamic.

    We can use manual serialization without dynamic.

    class UserModel {
      final String id;
      final String firstName;
      final String lastName;
      final String email;
      final String phoneNumber;
      final String? createdDate;
      final String? membershipType;
    
      UserModel({
        required this.id,
        required this.firstName,
        required this.lastName,
        required this.email,
        required this.phoneNumber,
        this.createdDate,
        this.membershipType,
      });
    
      
      factory UserModel.fromJson(Map<String, Object?> json) {
        
        return UserModel(
          id: json['id'] as String? ?? '', // you can use default values like this
          firstName: json['firstName'] as String? ?? '',
          lastName: json['lastName'] as String? ?? '',
          email: json['email'] as String? ?? '',
          phoneNumber: json['phoneNumber'] as String? ?? '',
          createdDate: json['createdDate'] as String?,  // Nullable values
          membershipType: json['membershipType'] as String?,  // Nullable values
        );
      }
    
      
      Map<String, Object?> toJson() {
        return {
          'id': id,
          'firstName': firstName,
          'lastName': lastName,
          'email': email,
          'phoneNumber': phoneNumber,
          'createdDate': createdDate,
          'membershipType': membershipType,
        };
      }
    }
    

    look at this code so I use empty value as default values. firstly we get it as string and when it's null we can set default values. typically we cannot set data from data base because those are come from backend that's why we use empty values as default values.