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
For a named parameter, if you don’t indicate that it’s
required
, then it could benull
.Change it to:
You are getting this error because of
null safety. And you define
String email;` as Nonnull.To Resolve:
and