skip to Main Content

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?

2

Answers


  1. Unfortunately, it is currently not possible, except by using code generators or manually. There is an experimental proposal called Dart macros:

    "The Dart macro system is a major new language feature currently under development which adds support for static meta-programming to the Dart language.

    A Dart macro is a user-definable piece of code that takes in other code as parameters and operates on it in real-time to create, modify, or add declarations."

    In the documentation, you can see how to install and test it, and the first example of a use case is this type of situation. Using the JsonCodable annotation allows your class to have the fromJson and toJson methods generated automatically.

    More information at: Dart Macros Documentation

    Login or Signup to reply.
  2. 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.

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