-2

The application has the following screens,

  1. Tab Screen (Main/Home Screen)
  2. CategoriesScreen( 1st tab Screen)
  3. Favorite Screen (2nd Tab screen)
  4. CategoryMealsScreen(child Screen of CategoriesScreen)

I have added print statement for each screen Build method to check on the navigation,

When I navigate back from "CategoryMealsScreen" to "CategoriesScreen", I can see that "CategoryMealsScreen" is once again being called why is that so?

Logs :

I/flutter (30320): TabsScreen  ( parent screen )

I/flutter (30320): CategoriesScreen

I/flutter (30320): CategoryMealsScreen ( child screen, pressed back in this screen)

I/flutter (30320): CategoryMealsScreen ( child screen, once again it's being called after landing in CategoriesScreen)

I/flutter (30320): TabsScreen  ( parent screen )

I/flutter (30320): CategoriesScreen

I/flutter (30320): CategoryMealsScreen ( child screen)

I/flutter (30320): CategoryMealsScreen ( child screen, once again it's being called after landing in CategoriesScreen)

I/flutter (30320): TabsScreen ( parent screen )

I/flutter (30320): CategoriesScreen

I/flutter (30320): TabsScreen

I/flutter (30320): FavoritesScreen

I/flutter (30320): TabsScreen ( parent screen )

I/flutter (30320): CategoriesScreen 

I/flutter (30320): CategoryMealsScreen  ( child screen)

I/flutter (30320): CategoryMealsScreen ( i pressed back button in-app bar and navigated back to parent  screen but once again child screen was printed in logs )

Below is the code that pushes the CategoryMealsScreen from CategoriesScreen,

Navigator.of(ctx).pushNamed(
      CategoryMealsScreen.routeName,
      arguments: {
        'id': id,
        'title': title,
      },
    );

My expectation was "CategoryMealsScreen" would not be called again when I navigate back to "TabsScreen"

Thanks in Advance.

D. Lawrence
  • 943
  • 1
  • 10
  • 23
Srinath
  • 93
  • 1
  • 8

1 Answers1

0

"Navigating back" in Flutter is done using navigation.pop(). This goes back one step in the navigation history. If you want to go back several steps, you can use this:

Navigator.of(context).popUntil((route) => route.isFirst);

This navigates back to the root of your widget tree (from your description I assume that this is what you want).

Widgets being rebuilt seems to be the intended behavior in flutter, see: https://github.com/flutter/flutter/issues/11655

Here are some thoughts about handling unwanted rebuilds: https://stackoverflow.com/a/52249579/2102328

Schnodderbalken
  • 3,257
  • 4
  • 34
  • 60
  • Hi Schnodderbalken, Thanks for your answer , but my question is, I am navigating only one screen back, but when flutter is loading the previous screen in the history the screen that got poped build method is being called again why is that so? – Srinath May 19 '20 at 06:44