Search code examples
flutterdartdart-null-safety

List can't access an object before it is initialized in dart


I created a house object to have member in a list called 'favHouses'. But dart doesn't let me to add those members before i initialize in constructor. Here is my code and the error that i get.

List<House> favHouses = [houseAlanya];
House houseAlanya = new House(..); 

the error i get:

The instance member 'houseAlanya' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression dart(implicit_this_reference_in_initializer)


Solution

  • With Dart 2.12, you can use the late keyword for lazy initialization.

    late List<House> favHouses = [houseAlanya];