2

I imagine this is by design, but when Navigate.Push'ing to a new Page from a StatefulWidget, the original page is rebuilt on rendering the second. This is the sample code from https://flutter.io/cookbook/navigation/navigation-basics/, I have just added print statements to show activity and converted the first page to be Stateful.

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    home: FirstScreen(),
  ));
}

class FirstScreen extends StatefulWidget {
  @override
  FirstScreenState createState() {
    return new FirstScreenState();
  }
}

class FirstScreenState extends State<FirstScreen> {
  @override
  Widget build(BuildContext context) {
    print('Building Screen 1');
    return Scaffold(
      appBar: AppBar(
        title: Text('First Screen'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Launch screen'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondScreen()),
            );
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext   context) {
    print('Building Screen 2');
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

Output from this shows that on rendering the second page, the first is also then rebuilt:

I/flutter (20929): Building Screen 2
I/flutter (20929): Building Screen 1

I have an instance of this scenario where Screen 1 uses a FutureBuilder. I don't want to have to reload all the data every time that Screen 2 is rebuilt. Can't help but think I'm missing something fundamental as this seems illogical.

Jens R.
  • 158
  • 1
  • 8
  • I made a more generic question about this problem: https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build/52249579#52249579 – Rémi Rousselet Sep 10 '18 at 00:02
  • With respect, although related, this is not the same. Your question is related to how an individual page gets rebuilt - I'm asking about how a different page causes a widget on another to be rebuilt. – Jens R. Sep 10 '18 at 00:19
  • 1
    If you're curious about the reasons: Navigator is a widget too. Therefore when you do `Navigator.push` it internally does a `setState`. Which trigger a rebuild of the widget tree. Be it the new route or the old one. You shouldn't care about that though. The real solution is the one I liked :) – Rémi Rousselet Sep 10 '18 at 00:24
  • Does that answer all your questions or is there still something you don't understand? – Rémi Rousselet Sep 10 '18 at 00:28
  • It does answer why it happens (thank you) but I don't know that I necessarily agree with it - I can see that state on Page1 should rebuild on re-entry, but having it rebuild on 'exit' doesn't make sense to me. My Page1 loads a set of data, a FloatingActionButton pushes to Page2 to allow the user to add a new item that will then be shown on Page1 on the 'back' button. So having the Page1 load done in InitState wouldn't trigger on re-entry to show the new item. – Jens R. Sep 10 '18 at 00:49
  • If you want to get a result from your pushed route on the original one, `Navigator.push` returns a future that contains the value passed to `Navigator.pop(value)`. You should use that instead – Rémi Rousselet Sep 10 '18 at 00:56
  • As for the "build on exit": The thing is doing a push doesn't really removed the previous route from the tree. It just doesn't show it, but it's still there (hence the build). Or else you'd lose your state when reopening it. – Rémi Rousselet Sep 10 '18 at 00:56
  • If you have problems with passing data between routes I'd suggest creating a custom question for that. The content of this question isn't really related. StackOverflow doesn't really like discussions in comments. – Rémi Rousselet Sep 10 '18 at 01:01

0 Answers0