Skip to content

call ModalRoute.of(context) causes route created twice in flutter 3.19 #145136

@wanjm

Description

@wanjm

Steps to reproduce

run the demo code and click the button on first page with or without the line "ModalRoute.of(context); "

Expected results

the log result is unchanged no matter whether there is the line; and should be the same as other release of flutter;

Actual results

with the line in 3.19 three lines are printed

flutter: reoute create 1 screen
flutter: reoute create 2 screen
flutter: reoute create 1 screen

in other release of flutter or without that line

flutter: reoute create 1 screen
flutter: reoute create 2 screen

Code sample

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

void main() {
  runApp(
    MaterialApp(
      title: 'Named Routes Demo',
      routes: {
        '/': route("/"),
        '/second': route("/second"),
      },
    ),
  );
}

WidgetBuilder route(String name){
  return (context) {
    ModalRoute.of(context);  //this line casue the problem;
    if(name == "/"){
      return FirstScreen();
    }else if(name == "/second"){
      return SecondScreen();
    }
    throw "route not found";
  };
}


class FirstScreen extends StatelessWidget {
  FirstScreen({super.key}) {
    print("reoute create 1 screen");
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          // Within the `FirstScreen` widget
          onPressed: () {
            // Navigate to the second screen using a named route.
            Navigator.pushNamedAndRemoveUntil(context, '/second',(a)=>false);
          },
          child: const Text('Launch screen'),
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  SecondScreen({super.key}) {
    print("reoute create 2 screen");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Screen'),
      ),
      body:const Center(
        child: Text("second screen"),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[Paste your output here]

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: regressionIt was better in the past than it is nowf: routesNavigator, Router, and related APIs.found in release: 3.19Found to occur in 3.19found in release: 3.21Found to occur in 3.21frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: solvedIssue is closed as solvedteam-frameworkOwned by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions