How to navigate in flutter during widget build?

StackOverflower

I'm trying to detect that user is no longer authenticated and redirect user to login. This is how I'm doing it

  Widget build(BuildContext context) {
    return FutureBuilder(
        future: _getData(context),
        builder: (context, snapshot) {
          try {
            if (snapshot.hasError && _isAuthenticationError(snapshot.error)) {
                Navigator.push(context, MaterialPageRoute(builder: (context) => LoginView()));
            }

Unfortunately doing navigation on build is not working. It throws this error

flutter: setState() or markNeedsBuild() called during build.
flutter: This Overlay widget cannot be marked as needing to build because the framework is already in the
flutter: process of building widgets.  A widget can be marked as needing to be built during the build 

I cannot just return LoginView widget since parent widget containts app bar and floating button and login view needs to be displayed without these controlls.. I need to navigate.

Is it possible to do it?

Abion47

Wrap it in Future.microtask. This will schedule it to happen on the next async task cycle (i.e. after build is complete).

Future.microtask(() => Navigator.push(
  context, 
  MaterialPageRoute(builder: (context) => LoginView())
));

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to access Stateful widget animation controller from another widget in Flutter?

분류에서Dev

How to get the data after we Navigate to another page in flutter

분류에서Dev

How can I paint a Widget on a Canvas in Flutter?

분류에서Dev

How to build a card screen with expansion panel list in Flutter?

분류에서Dev

Flutter Error "Could not navigate to inital root in my app"

분류에서Dev

Passing symbol values to the Rust compiler during build

분류에서Dev

maven: fetch resources from network during build

분류에서Dev

docker git clone during build is not cloning (and not erroring)

분류에서Dev

Flutter Widget 빌드 및 구조

분류에서Dev

How to update widget?

분류에서Dev

how to render a react navigate instead of a component?

분류에서Dev

How to navigate to the next page after completing the animation?

분류에서Dev

How to navigate different tabs from controller in Codeigniter

분류에서Dev

How to click a button and then navigate to a specific folder?

분류에서Dev

Angular - how to stop using hash links to navigate?

분류에서Dev

Cordova: How to navigate to page on success of REST response

분류에서Dev

How to navigate up a folder in VS Code

분류에서Dev

C++ macro defined in Eclipse is not used during build

분류에서Dev

Different home directory during Docker build in Docker hub

분류에서Dev

During "make" process for AOSP build, Can I check a progress ratio?

분류에서Dev

how to open CLI during installation

분류에서Dev

Flutter: Is there any way of using a widget only on ios but not on android?

분류에서Dev

How generate a token widget on Yesod?

분류에서Dev

how to call an intent from widget

분류에서Dev

How to use getSystemService in widget on android?

분류에서Dev

SDK Here Navigate Flutter-null 값이있는 Maneuver 객체

분류에서Dev

How to run a script only during first install of a package and not during upgrades?

분류에서Dev

Is it possible to construct a link, navigate to it and print the response in C# programmatically? How?

분류에서Dev

How do I navigate the org-mode clock history menu?

Related 관련 기사

  1. 1

    How to access Stateful widget animation controller from another widget in Flutter?

  2. 2

    How to get the data after we Navigate to another page in flutter

  3. 3

    How can I paint a Widget on a Canvas in Flutter?

  4. 4

    How to build a card screen with expansion panel list in Flutter?

  5. 5

    Flutter Error "Could not navigate to inital root in my app"

  6. 6

    Passing symbol values to the Rust compiler during build

  7. 7

    maven: fetch resources from network during build

  8. 8

    docker git clone during build is not cloning (and not erroring)

  9. 9

    Flutter Widget 빌드 및 구조

  10. 10

    How to update widget?

  11. 11

    how to render a react navigate instead of a component?

  12. 12

    How to navigate to the next page after completing the animation?

  13. 13

    How to navigate different tabs from controller in Codeigniter

  14. 14

    How to click a button and then navigate to a specific folder?

  15. 15

    Angular - how to stop using hash links to navigate?

  16. 16

    Cordova: How to navigate to page on success of REST response

  17. 17

    How to navigate up a folder in VS Code

  18. 18

    C++ macro defined in Eclipse is not used during build

  19. 19

    Different home directory during Docker build in Docker hub

  20. 20

    During "make" process for AOSP build, Can I check a progress ratio?

  21. 21

    how to open CLI during installation

  22. 22

    Flutter: Is there any way of using a widget only on ios but not on android?

  23. 23

    How generate a token widget on Yesod?

  24. 24

    how to call an intent from widget

  25. 25

    How to use getSystemService in widget on android?

  26. 26

    SDK Here Navigate Flutter-null 값이있는 Maneuver 객체

  27. 27

    How to run a script only during first install of a package and not during upgrades?

  28. 28

    Is it possible to construct a link, navigate to it and print the response in C# programmatically? How?

  29. 29

    How do I navigate the org-mode clock history menu?

뜨겁다태그

보관