Reputation:
I am new in Dart. I am surprised why this OOPs not working!
class MicroPhone {
String name;
String color;
int model;
}
main() {
var mic = new MicroPhone();
mic.name = " Blue Yeti";
mic.color = "Silver";
mic.model = 1234;
print(mic);
}
in output said :
test.dart:3:10: Error: Field 'name' should be initialized because its type 'String' doesn't allow null.
String name;
^^^^
test.dart:4:10: Error: Field 'color' should be initialized because its type 'String' doesn't allow null.
String color;
^^^^^
test.dart:5:7: Error: Field 'model' should be initialized because its type 'int' doesn't allow null.
int model;
^^^^^
Can you explain that why?
Upvotes: 0
Views: 292
Reputation: 44220
You're apparently using the Non-Null By Default (NNBD) version of Dart. The three member variables need to be initialized to something that is not null (via a constructor which you don't have), or you have to tell Dart that it's ok for them to be null by following the types with question mark.
There is documentation on how to live in a pre-NNBD world at dart.dev, but it'd be better if you just get used to it this way for the future. See https://dart.dev/null-safety for details.
Upvotes: 1