Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The argument type 'ModalRoute<Object?>?' can't be assigned to the parameter type 'PageRoute<dynamic>' #62023

Open
Yetispapa opened this issue Jul 22, 2020 · 10 comments
Labels
c: crash Stack traces logged to the console d: api docs Issues with https://api.flutter.dev/ f: material design flutter/packages/flutter/material repository. f: routes Navigator, Router, and related APIs. found in release: 3.3 Found to occur in 3.3 found in release: 3.7 Found to occur in 3.7 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list team-framework Owned by Framework team triaged-framework Triaged by Framework team

Comments

@Yetispapa
Copy link

Yetispapa commented Jul 22, 2020

Steps to Reproduce:

1.) setup RouteObservers on a widget (lets call it MyWidget) like in example: https://api.flutter.dev/flutter/widgets/RouteObserver-class.html
2.) use showGeneralDialog to present MyWidget

Expected results:

MyWidget should subscribe to RouteObservers without throwing an exception

Actual results:

exception is throwed: type '_DialogRoute<void>' is not a subtype of type 'PageRoute<dynamic>'

Exception is throwed here:

routeObserver
    .subscribe(this, ModalRoute.of(context));

flutter doctor -v

[✓] Flutter (Channel stable, v1.17.5, on Mac OS X 10.15.5 19F101, locale en-GB)
    • Flutter version 1.17.5 at /Users/myuser/sdk/flutter
    • Framework revision 8af6b2f038 (3 weeks ago), 2020-06-30 12:53:55 -0700
    • Engine revision ee76268252
    • Dart version 2.8.4

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/myuser/Library/Android/sdk
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.6, Build version 11E708
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

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

[✓] Connected device (1 available)
    • SM A600FN • 520085adecae3553 • android-arm • Android 10 (API 29)

• No issues found!

flutter run --verbose

logs
[ +615 ms] I/flutter (12837): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
[        ] I/flutter (12837): The following _TypeError was thrown building Builder:
[        ] I/flutter (12837): type '_DialogRoute<void>' is not a subtype of type 'PageRoute<dynamic>'
[        ] I/flutter (12837): 
[        ] I/flutter (12837): The relevant error-causing widget was:
[        ] I/flutter (12837):   SlideTransition file:///Users/myuser/Flutter/myapp/lib/utils/router.dart:67:16
[        ] I/flutter (12837): 
[        ] I/flutter (12837): When the exception was thrown, this was the stack:
[        ] I/flutter (12837): #0      MyWidgetState.didChangeDependencies (package:myapp/base/my_widget.dart:38:26)
[        ] I/flutter (12837): #1      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4661:12)
[        ] I/flutter (12837): #2      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
[        ] I/flutter (12837): ...     Normal element mounting (106 frames)
[        ] I/flutter (12837): #108    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
[        ] I/flutter (12837): #109    Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
[        ] I/flutter (12837): #110    RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5580:32)
[        ] I/flutter (12837): #111    MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5957:17)
[   +3 ms] I/flutter (12837): #112    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
[        ] I/flutter (12837): #113    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
[        ] I/flutter (12837): #114    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
[        ] I/flutter (12837): #115    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
[        ] I/flutter (12837): #116    StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
[        ] I/flutter (12837): #117    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
[        ] I/flutter (12837): #118    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
[        ] I/flutter (12837): #119    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
[   +7 ms] I/flutter (12837): #120    ProxyElement.update (package:flutter/src/widgets/framework.dart:4862:5)
[        ] I/flutter (12837): #121    _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:181:11)
[        ] I/flutter (12837): #122    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
[        ] I/flutter (12837): #123    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
[        ] I/flutter (12837): #124    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
[        ] I/flutter (12837): #125    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
[        ] I/flutter (12837): #126    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
[        ] I/flutter (12837): #127    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
[   +3 ms] I/flutter (12837): #128    StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
[        ] I/flutter (12837): #129    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
[        ] I/flutter (12837): #130    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
[        ] I/flutter (12837): #131    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
[        ] I/flutter (12837): #132    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
[        ] I/flutter (12837): #133    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
[        ] I/flutter (12837): #134    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
[        ] I/flutter (12837): #135    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
[ +322 ms] I/flutter (12837): #136    StatelessElement.update (package:flutter/src/widgets/framework.dart:4583:5)
[        ] I/flutter (12837): #137    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
[        ] I/flutter (12837): #138    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
[        ] I/flutter (12837): #139    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
[        ] I/flutter (12837): #140    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
[        ] I/flutter (12837): #141    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2627:33)
[        ] I/flutter (12837): #142    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:883:20)
[        ] I/flutter (12837): #143    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:284:5)
[   +3 ms] I/flutter (12837): #144    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1113:15)
[        ] I/flutter (12837): #145    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1052:9)
[        ] I/flutter (12837): #146    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:968:5)
[        ] I/flutter (12837): #150    _invoke (dart:ui/hooks.dart:261:10)
[        ] I/flutter (12837): #151    _drawFrame (dart:ui/hooks.dart:219:3)
[        ] I/flutter (12837): (elided 3 frames from dart:async)
[        ] I/flutter (12837): 
[        ] I/flutter (12837): ════════════════════════════════════════════════════════════════════════════════════════════════════
@TahaTesser
Copy link
Member

Hi @Yetispapa
Can you please provide your flutter doctor -v your flutter run --verbose and a complete reproducible minimal code sample
Thank you

@TahaTesser TahaTesser added in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds labels Jul 22, 2020
@Yetispapa
Copy link
Author

@TahaTesser Sure. I updated my issue accordingly.

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jul 22, 2020
@TahaTesser
Copy link
Member

Hi @Yetispapa
To better address the issue a complete reproducible minimal code sample, since it's not from steps
Thank you

@TahaTesser TahaTesser added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jul 23, 2020
@Yetispapa
Copy link
Author

@TahaTesser, Here you go:

import 'package:flutter/material.dart';

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

final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      navigatorObservers: [routeObserver],
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}


class RouteAwareWidget extends StatefulWidget {
  State<RouteAwareWidget> createState() => RouteAwareWidgetState();
}

// Implement RouteAware in a widget's state and subscribe it to the RouteObserver.
class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context));
  }

  @override
  void dispose() {
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didPush() {
    // Route was pushed onto navigator and is now topmost route.
  }

  @override
  void didPopNext() {
    // Covering route was popped off the navigator.
  }

  @override
  Widget build(BuildContext context) => Container();

}

class MyHomePage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
      floatingActionButton: FloatingActionButton(
        onPressed: (){

          showGeneralDialog<void>(
            useRootNavigator: false,
            barrierLabel: "modal",
            barrierDismissible: true,
            barrierColor: Colors.black.withOpacity(0.5),
            transitionDuration: Duration(milliseconds: 400),
            context: context,
            pageBuilder: (context, anim1, anim2) {
              return RouteAwareWidget();
            },
            transitionBuilder: (context, anim1, anim2, child) {
              return SlideTransition(
                position: Tween(begin: Offset(0, 1), end: Offset(0, 0)).animate(anim1),
                child: child,
              );
            },
          );

        },
        tooltip: 'Show Dialog',
        child: Icon(Icons.present_to_all),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

}

Its right after creating a flutter app. So you can just copy and paste it in main.dart. Let me know if you need more.

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jul 23, 2020
@TahaTesser
Copy link
Member

TahaTesser commented Jul 24, 2020

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

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

final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      navigatorObservers: [routeObserver],
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class RouteAwareWidget extends StatefulWidget {
  State<RouteAwareWidget> createState() => RouteAwareWidgetState();
}

// Implement RouteAware in a widget's state and subscribe it to the RouteObserver.
class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context));
  }

  @override
  void dispose() {
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didPush() {
    // Route was pushed onto navigator and is now topmost route.
  }

  @override
  void didPopNext() {
    // Covering route was popped off the navigator.
  }

  @override
  Widget build(BuildContext context) => Container();
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showGeneralDialog<void>(
            useRootNavigator: false,
            barrierLabel: "modal",
            barrierDismissible: true,
            barrierColor: Colors.black.withOpacity(0.5),
            transitionDuration: Duration(milliseconds: 400),
            context: context,
            pageBuilder: (context, anim1, anim2) {
              return RouteAwareWidget();
            },
            transitionBuilder: (context, anim1, anim2, child) {
              return SlideTransition(
                position: Tween(begin: Offset(0, 1), end: Offset(0, 0))
                    .animate(anim1),
                child: child,
              );
            },
          );
        },
        tooltip: 'Show Dialog',
        child: Icon(Icons.present_to_all),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

logs
Launching lib/main.dart on iPhone 11 in debug mode...
Xcode build done.                                           29.8s
Connecting to VM Service at ws://127.0.0.1:52844/hBnWFf2Kkqw=/ws

�[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════�[39;49m
�[38;5;244mThe following _TypeError was thrown building Builder:�[39;49m
type '_DialogRoute<void>' is not a subtype of type 'PageRoute<dynamic>'

�[38;5;244mThe relevant error-causing widget was�[39;49m
    �[38;5;248mSlideTransition�[39;49m
�[38;5;244mWhen the exception was thrown, this was the stack�[39;49m
�[38;5;248m#0      RouteAwareWidgetState.didChangeDependencies�[39;49m
�[38;5;244m#1      StatefulElement._firstBuild�[39;49m
�[38;5;244m#2      ComponentElement.mount�[39;49m
�[38;5;244m...     Normal element mounting (123 frames)�[39;49m
�[38;5;244m#125    Element.inflateWidget�[39;49m
�[38;5;244m...�[39;49m
�[38;5;248m════════════════════════════════════════════════════════════════════════════════�[39;49m
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building Builder:
type '_DialogRoute<void>' is not a subtype of type 'PageRoute<dynamic>'
The relevant error-causing widget was:
  �[38;5;248mSlideTransition�[39;49m
When the exception was thrown, this was the stack:
�[38;5;248m#0      RouteAwareWidgetState.didChangeDependencies�[39;49m
�[38;5;244m#1      StatefulElement._firstBuild�[39;49m
�[38;5;244m#2      ComponentElement.mount�[39;49m
...     Normal element mounting (123 frames)
�[38;5;244m#125    Element.inflateWidget�[39;49m
�[38;5;244m#126    Element.updateChild�[39;49m
�[38;5;244m#127    RenderObjectElement.updateChildren�[39;49m
�[38;5;244m#128    MultiChildRenderObjectElement.update�[39;49m
�[38;5;244m#129    Element.updateChild�[39;49m
�[38;5;244m#130    ComponentElement.performRebuild�[39;49m
�[38;5;244m#131    StatefulElement.performRebuild�[39;49m
�[38;5;244m#132    Element.rebuild�[39;49m
�[38;5;244m#133    StatefulElement.update�[39;49m
�[38;5;244m#134    Element.updateChild�[39;49m
�[38;5;244m#135    ComponentElement.performRebuild�[39;49m
�[38;5;244m#136    Element.rebuild�[39;49m
�[38;5;244m#137    ProxyElement.update�[39;49m
�[38;5;244m#138    _InheritedNotifierElement.update�[39;49m
�[38;5;244m#139    Element.updateChild�[39;49m
�[38;5;244m#140    SingleChildRenderObjectElement.update�[39;49m
�[38;5;244m#141    Element.updateChild�[39;49m
�[38;5;244m#142    ComponentElement.performRebuild�[39;49m
�[38;5;244m#143    StatefulElement.performRebuild�[39;49m
�[38;5;244m#144    Element.rebuild�[39;49m
�[38;5;244m#145    StatefulElement.update�[39;49m
�[38;5;244m#146    Element.updateChild�[39;49m
�[38;5;244m#147    SingleChildRenderObjectElement.update�[39;49m
�[38;5;244m#148    Element.updateChild�[39;49m
�[38;5;244m#149    SingleChildRenderObjectElement.update�[39;49m
�[38;5;244m#150    Element.updateChild�[39;49m
�[38;5;244m#151    ComponentElement.performRebuild�[39;49m
�[38;5;244m#152    Element.rebuild�[39;49m
�[38;5;244m#153    StatelessElement.update�[39;49m
�[38;5;244m#154    Element.updateChild�[39;49m
�[38;5;244m#155    ComponentElement.performRebuild�[39;49m
�[38;5;244m#156    Element.rebuild�[39;49m
�[38;5;244m#157    ProxyElement.update�[39;49m
�[38;5;244m#158    Element.updateChild�[39;49m
�[38;5;244m#159    ComponentElement.performRebuild�[39;49m
�[38;5;244m#160    StatefulElement.performRebuild�[39;49m
�[38;5;244m#161    Element.rebuild�[39;49m
�[38;5;244m#162    BuildOwner.buildScope�[39;49m
�[38;5;244m#163    WidgetsBinding.drawFrame�[39;49m
�[38;5;244m#164    RendererBinding._handlePersistentFrameCallback�[39;49m
�[38;5;244m#165    SchedulerBinding._invokeFrameCallback�[39;49m
�[38;5;244m#166    SchedulerBinding.handleDrawFrame�[39;49m
�[38;5;244m#167    SchedulerBinding._handleDrawFrame�[39;49m
�[38;5;244m#171    _invoke  (dart:ui/hooks.dart:253:10)�[39;49m
�[38;5;244m#172    _drawFrame  (dart:ui/hooks.dart:211:3)�[39;49m
(elided 3 frames from dart:async)
════════════════════════════════════════════════════════════════════════════════════════════════════

flutter doctor -v
[✓] Flutter (Channel dev, 1.21.0-1.0.pre, on Mac OS X 10.15.6 19G73, locale
    en-GB)
    • Flutter version 1.21.0-1.0.pre at /Users/tahatesser/Code/flutter_dev
    • Framework revision f25bd9c55c (9 days ago), 2020-07-14 20:26:01 -0400
    • Engine revision 99c2b3a245
    • Dart version 2.9.0 (build 2.9.0-21.0.dev 20bf2fcf56)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/tahatesser/Code/sdk
    • Platform android-30, build-tools 30.0.1
    • ANDROID_HOME = /Users/tahatesser/Code/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.6, Build version 11E708
    • CocoaPods version 1.9.3

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

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

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

[✓] Connected device (5 available)
    • Taha’s iPhone (mobile) • 00008020-001059882212002E            • ios
      • iOS 13.6
    • iPhone 11 (mobile)     • 7863E29F-EEE8-499E-BB28-6B111B76E3EE • ios
      • com.apple.CoreSimulator.SimRuntime.iOS-13-6 (simulator)
    • macOS (desktop)        • macos                                • darwin-x64
      • Mac OS X 10.15.6 19G73
    • Web Server (web)       • web-server                           •
      web-javascript • Flutter Tools
    • Chrome (web)           • chrome                               •
      web-javascript • Google Chrome 84.0.4147.89

• No issues found!

@TahaTesser TahaTesser added f: material design flutter/packages/flutter/material repository. found in release: 1.17 Found to occur in 1.17 found in release: 1.21 Found to occur in 1.21 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on c: crash Stack traces logged to the console and removed in triage Presently being triaged by the triage team labels Jul 24, 2020
@abozanona
Copy link

Hi @Yetispapa . Did you manage to fix this issue?

@kevindqc
Copy link

kevindqc commented Apr 21, 2021

Using

routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute);

seems to work for me.

It might not work if you are using that code inside a widget that is displayed by a ModalRoute but not a PageRoute (ie: from showDialog())

@maheshmnj
Copy link
Member

maheshmnj commented Dec 15, 2022

I can reproduce the issue though, You will need to typecase ModalRoute to PageRoute as mentioned above. I believe this is more of a documentation issue rather than a bug.

image

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

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

final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      navigatorObservers: [routeObserver],
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

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

  @override
  State<RouteAwareWidget> createState() => RouteAwareWidgetState();
}

// Implement RouteAware in a widget's state and subscribe it to the RouteObserver.
class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context)! as PageRoute);
  }

  @override
  void dispose() {
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didPush() {
    // Route was pushed onto navigator and is now topmost route.
  }

  @override
  void didPopNext() {
    // Covering route was popped off the navigator.
  }

  @override
  Widget build(BuildContext context) => Container();
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showGeneralDialog<void>(
            useRootNavigator: false,
            barrierLabel: "modal",
            barrierDismissible: true,
            barrierColor: Colors.black.withOpacity(0.5),
            transitionDuration: const Duration(milliseconds: 400),
            context: context,
            pageBuilder: (context, anim1, anim2) {
              return RouteAwareWidget();
            },
            transitionBuilder: (context, anim1, anim2, child) {
              return SlideTransition(
                position: Tween(begin: const Offset(0, 1), end: const Offset(0, 0))
                    .animate(anim1),
                child: child,
              );
            },
          );
        },
        tooltip: 'Show Dialog',
        child: const Icon(Icons.present_to_all),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
flutter doctor -v (mac)
[✓] Flutter (Channel master, 3.7.0-3.0.pre.33, on macOS 12.6 21G115 darwin-arm64, locale en-IN)
    • Flutter version 3.7.0-3.0.pre.33 on channel master 
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5201856805 (38 minutes ago), 2022-12-05 18:27:21 -0800
    • Engine revision a309d239c4
    • Dart version 2.19.0 (build 2.19.0-463.0.dev)
    • DevTools version 2.20.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly
      to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0-rc4
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.3

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

[✓] Android Studio (version 2021.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 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

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

[✓] Connected device (3 available)
    • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm64
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 108.0.5359.94

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
[✓] Flutter (Channel stable, 3.3.9, on macOS 12.6 21G115 darwin-arm, locale en-IN)
    • Flutter version 3.3.9 on channel stable at /Users/mahesh/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b8f7f1f986 (24 hours ago), 2022-11-23 06:43:51 +0900
    • Engine revision 8f2221fbef
    • Dart version 2.18.5
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0-rc4
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.3

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

[✓] Android Studio (version 2021.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 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

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

[✓] Connected device (3 available)
    • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 107.0.5304.110

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

cc: @chunhtai

@maheshmnj maheshmnj added f: routes Navigator, Router, and related APIs. found in release: 3.3 Found to occur in 3.3 found in release: 3.7 Found to occur in 3.7 d: api docs Issues with https://api.flutter.dev/ and removed found in release: 1.17 Found to occur in 1.17 found in release: 1.21 Found to occur in 1.21 labels Dec 15, 2022
@maheshmnj maheshmnj changed the title Widget with RouteObserver not working with showGeneralDialog The argument type 'ModalRoute<Object?>?' can't be assigned to the parameter type 'PageRoute<dynamic>' Dec 15, 2022
@321paranoiawhy
Copy link

Hello, I find the RouteObserver is still wrong:

@override
 void didChangeDependencies() {
   super.didChangeDependencies();
   routeObserver.subscribe(this, ModalRoute.of(context)!);
 }

code above should be changed to:

routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute);

Also, when using as PageRoute, could there be an error in some case?

@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-design Owned by Design Languages team labels Jul 8, 2023
@flutter-triage-bot flutter-triage-bot bot added the triaged-design Triaged by Design Languages team label Jul 8, 2023
@flutter-triage-bot flutter-triage-bot bot removed the triaged-design Triaged by Design Languages team label May 23, 2024
@flutter-triage-bot
Copy link

This issue is missing a priority label. Please set a priority label when adding the triaged-design label.

@Piinks Piinks added team-framework Owned by Framework team and removed team-design Owned by Design Languages team labels May 29, 2024
@goderbauer goderbauer added P2 Important issues not at the top of the work list triaged-framework Triaged by Framework team labels Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: crash Stack traces logged to the console d: api docs Issues with https://api.flutter.dev/ f: material design flutter/packages/flutter/material repository. f: routes Navigator, Router, and related APIs. found in release: 3.3 Found to occur in 3.3 found in release: 3.7 Found to occur in 3.7 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list team-framework Owned by Framework team triaged-framework Triaged by Framework team
Projects
None yet
Development

No branches or pull requests

9 participants