1

I am trying to set JSON values in My Model Class. But its returning error as Invalid argument(s) (input): Must not be null

UserAccount.dart

import 'package:json_annotation/json_annotation.dart';
part 'userAccount.g.dart';


@JsonSerializable(nullable: true,disallowUnrecognizedKeys:true,includeIfNull:true)
class UserAccount{
     String id;
     String name;
     String email;
     String profile_pic;
     String country;
     String phoneNumber;
     bool isNewUser;
     int role;
     int account_status;
     String token;
     DateTime token_expiry;
     String facebook_profile_url;
     String google_profile_url;
     String twitter_profile_url;
     int matchStixx_count;
     DateTime created_at;
     DateTime updated_at;


     UserAccount({
          this.id,
          this.name,
          this.email,
          this.profile_pic,
          this.country,
          this.phoneNumber,
          this.isNewUser,
          this.role,
          this.account_status,
          this.token,
          this.token_expiry,
          this.facebook_profile_url,
          this.google_profile_url,
          this.twitter_profile_url,
          this.matchStixx_count,
          this.created_at,
         this.updated_at
     });

     factory UserAccount.fromJson(Map<String,dynamic> json) => _$UserAccountFromJson(json);

     Map<String,dynamic> toJson() =>_$UserAccountToJson(this);

}

UserAccount.g.dart

part of 'userAccount.dart';

UserAccount _$UserAccountFromJson(Map<String, dynamic> json) {
  return UserAccount(
      id: json['_id'] as String,
      name: json['name'] as String,
      email: json['email'] as String,
      profile_pic: json['profile_pic'] as String,
      country: json['country'] as String,
      phoneNumber: json['phoneNumber'] as String,
      isNewUser: json['isNewUser'] as bool,
      role: json['role'] as int,
      account_status: json['account_status'] as int,
      token: json['token'] as String,
      token_expiry: DateTime.parse(json['token_expiry'] as String),
      facebook_profile_url: json['facebook_profile_url'] as String,
      google_profile_url: json['google_profile_url'] as String,
      twitter_profile_url: json['twitter_profile_url'] as String,
      matchStixx_count: json['matchStixx_count'] as int,
      created_at: DateTime.parse(json['created_at'] as String),
      updated_at: DateTime.parse(json['updated_at'] as String)
  );
}

Map<String, dynamic> _$UserAccountToJson(UserAccount instance) =>
    <String, dynamic>{
      'id': instance.id,
      'name': instance.name,
      'email': instance.email,
      'profile_pic': instance.profile_pic,
      'country': instance.country,
      'phoneNumber': instance.phoneNumber,
      'isNewUser': instance.isNewUser,
      'role': instance.role,
      'account_status': instance.account_status,
      'token': instance.token,
      'token_expiry': instance.token_expiry,
      'facebook_profile_url': instance.facebook_profile_url,
      'google_profile_url': instance.google_profile_url,
      'twitter_profile_url': instance.twitter_profile_url,
      'matchStixx_count': instance.matchStixx_count,
      'created_at': instance.created_at,
      'updated_at': instance.updated_at,
    };

JSON Data - this JSON is stored in ( responseBody["userData"] )

{_id: 1, name:some name, email: [email protected], profile_pic: https://xyz/yzz.png, isNewUser: true, role: 1, account_status: 1, matchStixx_count: 20, created_at: 2020-05-15T06:41:24.528Z, __v: 0}

Init Of Model

userAccount = new UserAccount.fromJson(responseBody["userData"]); //geting error here

2 Answers 2

10

It would have been much easier if you posted the full stacktrace since it contains the information about which field are null:

Unhandled exception:
Invalid argument(s) (input): Must not be null
#0      _RegExp.firstMatch (dart:core-patch/regexp_patch.dart:222:24)
#1      DateTime.parse (dart:core/date_time.dart:282:22)
#2      _$UserAccountFromJson (file:///stackoverflow.dart:64:30)
#3      new UserAccount.fromJson (file:///stackoverflow.dart:47:7)
#4      main (file:///stackoverflow.dart:5:15)
#5      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#6      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

The problematic line in the code the following:

      token_expiry: DateTime.parse(json['token_expiry'] as String),

Since your json does not contain token_expiry then the result here will be null. But since DateTime.parse does not accept null as input your get the error "Must not be null".

For a fix you could change the line to something like this:

token_expiry: json['token_expiry'] == null ? null : DateTime.parse(json['token_expiry'] as String),

Which will then give the same error for the field updated_at so you should properly also do:

created_at: json['created_at'] == null ? null : DateTime.parse(json['created_at'] as String),
updated_at: json['updated_at'] == null ? null : DateTime.parse(json['updated_at'] as String));
Sign up to request clarification or add additional context in comments.

1 Comment

The code in "UserAccount.g.dart" should be auto generated when running flutter pub run build_runner right? Perhaps @JsonKey(defaultValue: DateTime.Now()) DateTime <field>; would create fallback values for the DateTime fields?
0

If anyone passing by and had the same issue you need to verify your variable names like if you have a Map<String, dynamic> then you need to verify the key name that's what happens to me hope this helps good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.