Home > front end >  Variables in initState() not initialized
Variables in initState() not initialized

Time:03-02

class RoutePage extends StatefulWidget {
 final LatLng? pickUp;
 final LatLng? dropOff;

 RoutePage({required this.pickUp, required this.dropOff});

 @override
 _RoutePage createState() => _RoutePage();
}

class _RoutePage extends State<RoutePage> {
 Set<Marker> _markers = {};
 LatLng _origin;
 LatLng _destination;

 void initState() {
   super.initState();
   final _origin = widget.pickUp;
   final _destination = widget.dropOff;
 }

 @override
 Widget build(BuildContext context) {
   ...
}

In my code, despite that I have initialized _origin and _destination in the initState(), I still get the error message "Non-nullable instance field '_origin' must be initialized." Adding late does not work too... I also tried adding setState, but it doesn't work as well.

CodePudding user response:

These variables are not initialised because you are recreating them inside the initState() function:

void initState() {
 super.initState();
 final _origin = widget.pickUp; // <-- Recreating _origin here
 final _destination = widget.dropOff; // <-- Recreating _destination here
}

It is allowed to have variables with the same name in Dart because inside functions you have a different scope. Thus, you get no error here, but since you are using the final keyword, you are recreating those variables. To resolve this, do not use final inside your initState() function:

void initState() {
 super.initState();
 _origin = widget.pickUp; // <-- Assigning a value to _origin here
 _destination = widget.dropOff; // <-- Assigning a value to _destination here 
}

CodePudding user response:

  1. pickUp, dropOff variables are typed as LatLng? then you are trying to assign to another variable type as LatLng
  2. In initState you are again creating variables.

updated code(above things are updated in the below code):

class RoutePage extends StatefulWidget {
  final LatLng? pickUp;
  final LatLng? dropOff;

  RoutePage({required this.pickUp, required this.dropOff});

  @override
  _RoutePage createState() => _RoutePage();
}

class _RoutePage extends State<RoutePage> {
  Set<Marker> _markers = {};
  LatLng? _origin;
  LatLng? _destination;

  @override
  void initState() {
    super.initState();
    _origin = widget.pickUp;
    _destination = widget.dropOff;
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

CodePudding user response:

final properties are assigned either via named constructor parameters or at declaration, not in the initState method.

You could initialize them via the named constructor parameters like this:


class RoutePage extends StatefulWidget {
 final LatLng? pickUp;
 final LatLng? dropOff;
 
 RoutePage({required this.pickUp, required this.dropOff});
 
 @override
 _RoutePage createState() => _RoutePage(origin: pickUp, destination: dropOff);
}

class _RoutePage extends State<RoutePage> {
 Set<Marker> _markers = {};
 final LatLng? origin;
 final LatLng? destination;
  
 _RoutePage({ this.origin, this.destination });

 void initState() {
   super.initState();
 }

  @override
  Widget build(BuildContext context) {
   // ... 
 }
}

Or don't set them final at all and then you can assign them via the initState. My two cents.

  • Related