Skip to content

Builders of MaterialPage and CupertinoPage are a footgun #66695

@goderbauer

Description

@goderbauer
class _KeepsStateTestWidgetState extends State<KeepsStateTestWidget> {
  String _subpage = 'subpage';

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      home: Navigator(
        key: widget.navigatorKey,
        pages: <Page<void>>[
          const CupertinoPage<void>(builder: (BuildContext context) => Text('home')),
          if (_subpage != null) CupertinoPage<void>((BuildContext context) => Text(_subpage)),
        ],
        onPopPage: (Route<dynamic> route, dynamic result) {
          if (!route.didPop(result)) {
            return false;
          }
          setState(() {
            _subpage = null;
          });
          return true;
        },
      ),
    );
  }
}

When the second page is transitioning out, _subpage is set to null. Because the Navigator is rebuild, it would rebuild all its routes as well with that the builder of the second executes again, rebuilding itself with _subpage == null, which is not what people want.

Metadata

Metadata

Assignees

Labels

P0Critical issues such as a build break or regressionf: routesNavigator, Router, and related APIs.frameworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions