I’m learning Flutter and I’m making an app as a demo, but I couldn’t solve this error. Even though I have given values to the parameter, when I run it, I get an error that it cannot be null.
student.dart code
class Student {
String firstName;
String lastName;
String status;
int grade;
Student(String firstName, String lastName, int grade) {
this.firstName = firstName;
this.lastName = lastName;
this.status = "Geçti";
this.grade = grade;
}
}
main.dart code
List<Student> students = [Student("Engin", "Demiroğ", 75), Student("İsmail", "Güner", 65), Student("Aykut", "Elmas", 88), Student("Cengiz", "Han", 100)];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Demo1"),
),
body: buildBody(context),
);
}
Widget buildBody(BuildContext context) {
return Column(
children: [
Expanded(
child: ListView.builder(
itemCount: students.length,
itemBuilder: (BuildContext context, int index) {
return Text(students[index].firstName);
}),
),
The error I got
6
Answers
Add the question mark with the type of each property to indicate that it is nullable:
Dart is null-safe, so since you’re declaring new properties of a class without the question mark, it will interpret that the value will never be null, but initially their values are all null, thus the error. I suggest to read more on the null safety feature since it’s a pretty important concept for Dart/ Flutter.
P/s: To use the nullable property (for example
String? firstName
, use it with??
for those widgets that don’t take in nullable value. For example:Here, the
??
has meaning that, ifstudent.firstName
is null, use thisEmpty name
string as value. SinceText
requires a non-null value, we need to make sure that in case thefirstName
is null, we have a default value in place to avoid passing nullable value in.Dart has recently introduced null-safety. It prevents you from making a lot of mistakes. For more info: https://dart.dev/null-safety/understanding-null-safety
The best way to solve your problem is to understand what variables can be nullable or non-nullable.
In this case, only the variable status can’t be null, and it’s value is set to "Geçti" by the constructor. All the other variables can be null.
Flutter can be serious concerning null values and initialization. Even if practically you assign values, it does not ‘believe’ you, and throws a build error. See here for example. But for as a quick fix you can mark the members of Student class as late, practically you promise to Flutter, that later you will assign values:
You have many other options, but this one works as a starter, I tried it.
From Dart 2.12 you need to specify if your parameters are nullable or vice versa. Please see below code
If you enable null safety, then you must initialize the values of non-nullable variables before you use them. Source: documentation for more info.
Null safety is enabled by default. As stated there, you need to assign a default value for those variables.
Your class can be rewritten using dart syntax sugar for initializing variables (documentation)
Using that syntax for initializing variables we don’t need to specify a default value for them, so removing the unnecesary default values:
It’s also common to declare variables initialized in the constructor as
final
(can only be initialized once)I recommend taking a look at the Dart language tour
This happens because the type of your variables are not nullable but you didn’t initialize them with a value. To fix this use the keyword late.