5

I have noticed that when I move between screens and push route e.g. with

Navigator.pushNamed(context, "/screenNumberTwo");

the build method on my source screen (let's call it screenNumberOne) gets called after the build method for the screenNumberTwo (though only screen number two is visible). I see this happening both if I move with onPressed in MaterialButton and with onTap in InkWell, in case of routes with parameters and without.

My routing is done as below

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        onGenerateTitle: (BuildContext context) =>
            Localization.of(context).appTitle,
        initialRoute: "/",
        routes: {
          "/": (context) => FirstScreen(),
          "/select_category": (context) => SecondScreen(),
        },
        onGenerateRoute: (routeSettings) {
          print("Route: ${routeSettings.name}");
          var path = routeSettings.name.split('/');

          if (path[1] == 'thirdscreen') {
            if (path.length == 3) {
            //(... where I set paramId)
              return new MaterialPageRoute(
                  builder: (context) => new ThirdScreen(paramId),
                  settings: routeSettings);
            } else if (path.length == 4) {
            //(... where I set paramId and param2Id)
              return new MaterialPageRoute(
                  builder: (context) => new ThirdScreen(
                        paramId, param2Id,
                      ),
                  settings: routeSettings);
            }
        },
        localizationsDelegates: [
          const LocalizationDelegate(),
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('en'),
          const Locale('pl'),
        ]);
  }
}

This is not a functional problem, more of a concern, that flutter in my case is burning CPU on calculating screen that is not present to the user anyway... Any suggestions what to change/where to look for problems, are appreciated!

Lech Migdal
  • 3,828
  • 5
  • 36
  • 63
  • Seems that animations are causing the build to be called. However, I didn't find a concrete explanation of this in the documentation. Maybe this answer might help: https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build – Oleksii Kalentiev Feb 01 '19 at 14:21

1 Answers1

0

This is normal. Flutter can call the build function for all sorts of reasons. It can also call it repeatedly in the case of animations, etc. That's why there should be no expensive computations in build. On the other hand, as this is at the core of the design of Flutter, the whole build mechanism is extremely efficient (for example the garbage collector is designed to cope with the creation of all those new, short-lived widget objects all the time).

To achieve 60FPS animations, Flutter will be calling build at that frame rate, so one extra unwanted build isn't going to matter.

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • 2
    I don't think you answered the OP's question. We all know build() can be called many times, but WHY for OLD screens that aren't currently active? I'm having the same issue. – Pete Alvin Dec 05 '19 at 19:56
  • @PeteAlvin , Check https://github.com/flutter/flutter/issues/11655 . A flutter contributor said "This is working as intended", same as Richard Heap in this answer. – encubos Apr 04 '20 at 22:53