0

I have a list of statefull widget which I use to navigate from pages. but when I was trying to push these List element in Navigator.push(), it says "Reading static variable during its initialization".

here's the list:

List<ActionScreen> fullBodyNavigation = [
          ActionScreen(title: 'demo data', number: 20, gifDirectory: "assets/gifs/demodata.gif", currentList: fullBodyNavigation,
              frameMin: 0, frameMax: 17, milisecondAnimation: 800, milisecondTimer: 800, sessionNumber: 1,),
          ActionScreen(title: 'demodata', number: 15, gifDirectory: "assets/gifs/demodata.gif", currentList: fullBodyNavigation,
              frameMin: 0, frameMax: 30, milisecondAnimation: 1200, milisecondTimer: 1200, sessionNumber: 2,),
          ActionScreen(title: 'demodata', number: 15, gifDirectory: "assets/gifs/demodata.gif", currentList: fullBodyNavigation,
              frameMin: 0, frameMax: 2, milisecondAnimation: 2000, milisecondTimer: 2000, sessionNumber: 3,),
          .............
        ]

here's the push method:

onPress: () {
      Navigator.push(context, MaterialPageRoute(builder: (context) => fullBodyNavigation[0],),);
},

I foud a solution in stackoverflow which said I have to use 'data' such as "fullBodyNavigation.data[0]". but ide said "getter data is not defined". can anyone pls help?

here's that solution's link: Reading static variable during its initialization | Flutter

1 Answer 1

2

The problem is in the way you have created the navigation pages list:

    List<ActionScreen> fullBodyNavigation = [
        ActionScreen(title: 'demo data', 
          number: 20, 
          gifDirectory: "assets/gifs/demodata.gif", 
          currentList: fullBodyNavigation,
          frameMin: 0, 
          frameMax: 17,
          milisecondAnimation: 800, 
          milisecondTimer: 800, 
          sessionNumber: 1,
        ),
        .............
    ]

Why do you want to pass currentList: fullBodyNavigation to ActionScreen. So basically, you are trying to access the list inside the list itself. The chicken and egg problem. THat's what compiler is warning you with the error.

To fix this, dont pass currentList: fullBodyNavigation to ActionScreen. Instead make fullBodyNavigation a static field and in ActionScreen just access it with the class name. That should do your thing and also make the compiler happy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.