skip to Main Content

I’m new to flutter and I’m working on http post request by creating a model.

class LoginResponseModel {
  final String token;
  final String error;

  LoginResponseModel({this.token, this.error});

  factory LoginResponseModel.fromJson(Map<String, dynamic> json) {
    return LoginResponseModel(
      token: json["token"] != null ? json["token"] : "",
      error: json["error"] != null ? json["error"] : "",
    );
  }
}

class LoginRequestModel {
  String email;
  String password;
  String tenant;

  LoginRequestModel({
    this.email,
    this.password,
    this.tenant,
  });

  Map<String, dynamic> toJson() {
    Map<String, dynamic> map = {
      'email': email.trim(),
      'password': password.trim(),
      'token':tenant.trim(),
    };

    return map;
  }
}

I am getting error in the following parts of this code :

LoginRequestModel({
    this.email,
    this.password,
    this.tenant,
  });

The parameter ’email’ can’t have a value of ‘null’ because of its type, but the implicit default value is ‘null’

Then I follow the path below when using this model I created to log in via the login page.

LoginRequestModel requestModel;
@override
void initState() {
super.initState();
requestModel = LoginRequestModel(email: '', password: '', tenant: ''); //After adding required, I have to enter these fields.

and when i send login request via button i see this in debug console.

{email: , password: , token: }

2

Answers


  1. For a named parameter, if you don’t indicate that it’s required, then it could be null.

    Change it to:

    LoginRequestModel({
        required this.email,
        required this.password,
        required this.tenant,
      });
    
    Login or Signup to reply.
  2. You are getting this error because of null safety. And you define String email;` as Nonnull.

    To Resolve:

    1. if your parameter should not keep null then add the required keyword inside the constructor like below.
    class LoginRequestModel {
      String email;
      String password;
      String tenant;
    
      LoginRequestModel({
        required this.email,
        required this.password,
        required this.tenant,
      });
    
      Map<String, dynamic> toJson() {
        Map<String, dynamic> map = {
          'email': email.trim(),
          'password': password.trim(),
          'token':tenant.trim(),
        };
    
        return map;
      }
    }
    
    

    and

    1. If you want your parameters can be null in the future then add the below code.
    class LoginRequestModel {
      String? email;
      String? password;
      String? tenant;
    
      LoginRequestModel({
         this.email,
         this.password,
         this.tenant,
      });
    
      Map<String, dynamic> toJson() {
        Map<String, dynamic> map = {
          'email': email?.trim(),
          'password': password?.trim(),
          'token':tenant?.trim(),
        };
    
        return map;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search