You can make _value nullable, by doing int? _value = 1;, or you can guarantee that val has a value when you setState, by doing _value = val ?? -1; which will set _value to -1 if val == null.
It’s due to null safety.
You are trying to assign a nullable integer int? to a non-nullable integer int, which is not allowed in Dart.
To solve this error, you can either change the data type of the variable val to int to make it non-nullable, or make sure that the value being assigned to _value is not null using _value = val!
Alternatively, you can use the null-aware assignment operator ??= to assign a default value to _value in case it is null, like this: _value = val ?? 0
(only if datatype is int?)
2
Answers
You can make
_value
nullable, by doingint? _value = 1;
, or you can guarantee thatval
has a value when yousetState
, by doing_value = val ?? -1;
which will set_value
to-1
ifval == null
.It’s due to null safety.
You are trying to assign a nullable integer int? to a non-nullable integer int, which is not allowed in Dart.
To solve this error, you can either change the data type of the variable val to int to make it non-nullable, or make sure that the value being assigned to _value is not null using _value = val!
Alternatively, you can use the null-aware assignment operator ??= to assign a default value to _value in case it is null, like this: _value = val ?? 0
(only if datatype is int?)