Reputation: 341
I'm getting a NoSuchMethodError: invalid member on null: 'user'
error when I execute the following. I assume I'm missing a constructor or setter or initializing wrong.
class Mac{
MacUser user;
Mac({
this.user
});
}
class MacUser{
String username;
String password;
MacUser({
this.username,
this.password,
});
}
void main(){
Mac mac;
mac.user.username = "root"; // NoSuchMethodError: invalid member on null: 'user'
mac.user.password = "root";
}
Upvotes: 1
Views: 1370
Reputation: 2802
Mac mac = new Mac(user: new MacUser()); // this line will Create a Null Object as you may or may not have both values at this time later on you can edit those value by below code
mac.user.username = "root";
mac.user.password = "root";
Upvotes: 0
Reputation: 6186
You're trying to set the property on a null object. You need to define some value first before changing:
Example:
Mac mac = new Mac(user: MacUser(username: "username", password: "password")); // initialize some value first
mac.user.username = "root";
mac.user.password = "root";
Upvotes: 1