Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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(),
  ];

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading