The instance member 'widget' can't be accessed in an initializer. flutter

I have a Future in my Stateful Widget that I want to pass on to another class.

The Problem is that this class is in another Widget.
So when I want to pass on the data I get the

The instance member ‘widget’ can’t be accessed in an initializer.

Exception

Here the Code:

class HomeScreen extends StatefulWidget {
  final Future<List> modelle;
  const HomeScreen({Key? key, required this.modelle,})
      : super(key: key);

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

class _HomeScreenState extends State<HomeScreen> {
  
  List<Widget> _widgetOptions = <Widget>[
    Favorites(),
    BodyHomeScreen(modelle: widget.modelle),
    Kontakt(),
  ];

The problem is the line where I want to access my List

widget.modelle

Any suggestions how I can fix this behaviour?

>Solution :

Setup the list inside initState() function.

late List<Widget> _widgetOptions ;

@override
initState(){
    _widgetOptions = <Widget>[
    Favorites(),
    BodyHomeScreen(modelle: widget.modelle),
    Kontakt(),
  ];

}

Leave a Reply