Search code examples
flutterscoped-model

Flutter ScopedModel error " Only static members can be accessed in initializers"


I want to pass the ScopedModel to another page. Or in the initState call the ScopedModel so I can use the data.

  final MainModel model;
  IndexPage(this.model);
  @override
  State<StatefulWidget> createState() {
    return IndexPageState();
  }
}

class IndexPageState extends State<IndexPage> {
  final pages = [
    HomePage(),
    CreatePage(),
    MapPage(widget.model),
  ];

When I do this I have this error:

Only static members can be accessed in initializers

Thanks!


Solution

  • Martinez, you would need to move the declaration of your pages inside initState()

    class IndexPageState extends State<IndexPage> {
    
    List<Widget> pages;
    
    @override
    void initState() {  
       pages = [
            HomePage(),
            CreatePage(),
           MapPage(widget.model),
          ];
    }