Skip to content

Page that use ModalRoute.of will trigger the build method when pushing or popping another page. #145387

@LinXunFeng

Description

@LinXunFeng

Steps to reproduce

  1. Run the example
  2. Click FAB to jump to Page2
  3. Click the return button of Page2 to return to MyHomePage

MyHomePage uses ModalRoute.of.

After upgrading to Flutter 3.19.0, the build method of MyHomePage will be called when pushing and popping Page2. This was not the case before 3.16.9.

I found the relevant code that caused the issue

@override
void didChangeNext(Route<dynamic>? nextRoute) {
super.didChangeNext(nextRoute);
changedInternalState();
}
@override
void didPopNext(Route<dynamic> nextRoute) {
super.didPopNext(nextRoute);
changedInternalState();
}

Releated PR: #134554

Expected results

Only print build once on console.

Actual results

The build will be printed three times.

Code sample

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final route = ModalRoute.of(context);
  }

  @override
  Widget build(BuildContext context) {
    debugPrint('build');
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('Flutter Demo Home Page'),
      ),
      body: const Center(
        child: Text('MyHomePage'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) {
                return Page2();
              },
            ),
          );
        },
      ),
    );
  }
}

class Page2 extends StatefulWidget {
  @override
  State<Page2> createState() => _Page2State();
}

class _Page2State extends State<Page2> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Page2'),
      ),
      body: const SizedBox.shrink(),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
  • 3.19.3
2024-03-19.17.56.36.mov
  • 3.16.9
2024-03-19.17.59.27.mov

Logs

Logs
3 flutter: build

Flutter Doctor output

Doctor output
fvm spawn 3.19.3 doctor -v
Spawning version "3.19.3"...
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.3 23D56 darwin-arm64 (Rosetta), locale zh-Hans-CN)
    • Flutter version 3.19.3 on channel stable at /Users/lxf/fvm/versions/3.19.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ba39319843 (12 days ago), 2024-03-07 15:22:21 -0600
    • Engine revision 2e4ba9c6fb
    • Dart version 3.3.1
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/lxf/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_SDK_ROOT = /Users/lxf/Library/Android/sdk
    • Java binary at: /usr/bin/java
    • Java version Java(TM) SE Runtime Environment (build 17.0.8+9-LTS-211)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode-15.2.0.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[!] Android Studio (version unknown)
    • Android Studio at /Applications/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to determine Android Studio version.
    ✗ android-studio-dir = /Applications/
    ✗ Android Studio not found at /Applications/Contents
    • Try updating or re-installing Android Studio.
    • Consider removing your android-studio-dir setting by running:
      flutter config --android-studio-dir=

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (3 available)
    • lxf的iPhone (mobile) • xxxxxxxx-xxxxxxxxxxxxxxxx • ios            • iOS 16.4.1 20E252
    • macOS (desktop)     • macos                     • darwin-arm64   • macOS 14.3 23D56 darwin-arm64 (Rosetta)
    • Chrome (web)        • chrome                    • web-javascript • Google Chrome 122.0.6261.129

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: duplicateIssue is closed as a duplicate of an existing issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions