Flutter initstate navigator is not working

前端 未结 1 742
情深已故
情深已故 2020-12-12 01:39

I am using initState and having Navigator issue:

I/flutter ( 5726): The following assertion was thrown building Builder: I/flutter ( 5726): setState

相关标签:
1条回答
  • 2020-12-12 02:37

    We are getting the error as while building the Widget itself we are asking to navigate.

    There is a work around for this.

    Future(() {
       Navigator.push(context, MaterialPageRoute(builder: (context)=> Dashboard()));
    });
    

    Explaination:

    As Dart is based on single-threaded event loop, when we create an async tasks, it will put those events in the end of the event queue and continue it's current execution. Please refer below example for more details,

    void main() {
      print("first");
      Future(() => print("second"));
      print("third");
      Future(() => print("forth"));
    }
    

    Output will be

    first
    third
    second
    forth
    
    0 讨论(0)
提交回复
热议问题