[Flutter/dart]LateInitializationError with late final variable
Phenomenon
There is a class like below.
class SomeClass{
late final String? hoge;
SomeClass({this.hoge}){
hoge??= ""; //1
}
}
If hoge is null (i.e. no value is set for the argument), I will get a lateInitialization error at point 1.
I thought it's okay to assign a value here, because it is "late"...
Cause
Since it is "initialized" at "this.hoge", subsequent value assignments will be lateInitialization, even if the value is null.
I thought that it was not initialized yet because it was not assigned if it was null.
Solution
class SomeClass{
late final String? hoge;
SomeClass({String? hoge}){
this.hoge = hoge?? "";
}
}
Recent Posts
See AllWhat want to do There is an operation that operates on two-dimensional arrays There are multiple targets to be processed In this case, I...
What want to do As stated in the title, I would like to define an array of function pointers. Specifically, for example, the third...
Phenomenon There is an array Check whether the index variable is within the size of the array, and access the element only if it is...
Comments