title: From StatefulWidget navigate to StatelessWidget runs the last builder but also the first builder
body: <p>Is there a way to prevent the <code>build()</code> from being called multiple times?</p>
  
  <p>The next code creates two pages, each with one button, the button on <code>FirstPage</code> navigates to <code>SecondPage</code>, and the one on <code>SecondsPage</code> goes back to <code>FirstPage</code>.</p>
  
  <p>The problem is that if you click the button on <code>FirstPage</code> it runs the <code>SecondPage</code> <code>build()</code> but it also runs the <code>FirstPage</code> <code>build()</code>.</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'First page',
        home: FirstPage(),
      );
    }
  }
  
  class FirstPage extends StatefulWidget {
    @override
    State&lt;StatefulWidget&gt; createState() {
      return _FirstPageState();
    }
  }
  
  class _FirstPageState extends State&lt;FirstPage&gt; {
    @override
    Widget build(BuildContext context) {
      print('---------- In FirstPage build ----------');
      return Scaffold(
        appBar: AppBar(
          title: Text('First page'),
        ),
        body: Column(
          children: [
            Text('First page body'),
            FlatButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) =&gt; SecondPage()),
                );
              },
              child: Text('Go to second page')
            )
          ]
        )
      );
    }
  }
  
  class SecondPage extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      print('---------- In SecondPage build ----------');
      return Scaffold(
        appBar: AppBar(
          title: Text('Second page'),
        ),
        body: Column(
          children: [
            Text('Second page body'),
            FlatButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Go back')
            )
          ]
        )
      );
    }
  }
  </code></pre>
  
  <p>Console output:</p>
  
  <pre><code>Performing hot restart...                                        
  D/dalvikvm(23908): threadid=12: interp stack at 0x76040000
  Restarted app in 3,849ms.
  I/flutter (23908): ---------- In FirstPage build ----------
  I/SurfaceTextureClient(23908): [STC::queueBuffer] (this:0x76b39810) fps:0.07, dur:14731.13, max:14731.13, min:14731.13
  I/SurfaceTextureClient(23908): [STC::queueBuffer] this:0x76b39810, api:1, last queue time elapsed:14731.13
  </code></pre>
  
  <p>After click button:</p>
  
  <pre><code>V/Provider/Settings(23908): from db cache, name = sound_effects_enabled , value = 0
  I/flutter (23908): ---------- In SecondPage build ----------
  I/SurfaceTextureClient(23908): [STC::queueBuffer] (this:0x76b39810) fps:0.24, dur:4140.60, max:4140.60, min:4140.60
  I/flutter (23908): ---------- In FirstPage build ----------
  </code></pre>
  
  <p>Thanks for your help.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I tryed using <code>popAndPushNamed()</code> on <code>FirstPage</code> it just runs the build on the <code>SecondPage</code> but if I try to go back with:</p>
  
  <pre><code>Navigator.popAndPushNamed(
      context,
      '/'
  );
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>I/flutter ( 5005): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
  I/flutter ( 5005): The following assertion was thrown while handling a gesture:
  I/flutter ( 5005): Cannot reuse a MaterialPageRoute&lt;dynamic&gt; after disposing it.
  I/flutter ( 5005): 'package:flutter/src/widgets/routes.dart': Failed assertion: line 215 pos 12:
  I/flutter ( 5005): '!_transitionCompleter.isCompleted'
  I/flutter ( 5005): Either the assertion indicates an error in the framework itself, or we should provide substantially
  I/flutter ( 5005): more information in this error message to help you determine and fix the underlying cause.
  I/flutter ( 5005): In either case, please report this assertion by filing a bug on GitHub:
  I/flutter ( 5005):   https://github.com/flutter/flutter/issues/new
  I/flutter ( 5005): When the exception was thrown, this was the stack:
  I/flutter ( 5005): #2      TransitionRoute.didPopNext (package:flutter/src/widgets/routes.dart)
  I/flutter ( 5005): #3      NavigatorState.pop (package:flutter/src/widgets/navigator.dart:1691:23)
  I/flutter ( 5005): #4      NavigatorState.popAndPushNamed (package:flutter/src/widgets/navigator.dart:1399:5)
  I/flutter ( 5005): #5      Navigator.popAndPushNamed (package:flutter/src/widgets/navigator.dart:731:34)
  I/flutter ( 5005): #6      SecondPage.build.&lt;anonymous closure&gt; (file:///home/ncs/Documents/flutter/src/test1/lib/main.dart:83:25)
  I/flutter ( 5005): #7      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:494:14)
  I/flutter ( 5005): #8      _InkResponseState.build.&lt;anonymous closure&gt; (package:flutter/src/material/ink_well.dart:549:30)
  I/flutter ( 5005): #9      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
  I/flutter ( 5005): #10     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
  I/flutter ( 5005): #11     TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:94:7)
  I/flutter ( 5005): #12     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
  I/flutter ( 5005): #13     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
  I/flutter ( 5005): #14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
  I/flutter ( 5005): #15     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
  I/flutter ( 5005): #16     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
  I/flutter ( 5005): #17     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
  I/flutter ( 5005): #18     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
  I/flutter ( 5005): #19     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
  I/flutter ( 5005): #20     _invoke1 (dart:ui/hooks.dart:134:13)
  I/flutter ( 5005): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
  I/flutter ( 5005): (elided 2 frames from class _AssertionError)
  I/flutter ( 5005): Handler: onTap
  I/flutter ( 5005): Recognizer:
  I/flutter ( 5005):   TapGestureRecognizer#94626(debugOwner: GestureDetector, state: possible, won arena, finalPosition:
  I/flutter ( 5005):   Offset(65.3, 119.6), sent tap down)
  I/flutter ( 5005): ════════════════════════════════════════════════════════════════════════════════════════════════════
  </code></pre>
  
  <p>The only way I'm able to remove the unwanted build is by having a condition like::</p>
  
  <pre><code>class _FirstPageState extends State&lt;FirstPage&gt; {
      bool _canBuild = true;
  
      @override
      Widget build(BuildContext context) {
          if (!_canBuild) {
              return Container();
          }
          print('---------- In FirstPage build ----------');
          ...
          onPressed: () {
              _canBuild = false;
              Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) =&gt; SecondPage()),
              ).then((_) {
                  _canBuild = true;
              });
          },
          ...
  </code></pre>
  
  <p>Is this a bad practice or can create any problems?</p>