Milad-Akarie / auto_route_library

Flutter route generator
MIT License
1.59k stars 403 forks source link

Unhandled type Exception for RoutePage #1732

Open ChivialK opened 1 year ago

ChivialK commented 1 year ago

I have a simple router like this:

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart' hide Route;
import 'package:flutter_template_auto_route/screens/content_screen.dart';
import 'package:flutter_template_auto_route/screens/home_screen.dart';
...
AutoRoute(
  path: '/home',
  page: HomeRoute.page,
  initial: true,
  children: [
    AutoRoute(path: 'content/:index', page: ContentRoute.page),
  ],
),
...

I want to pass something to content page, and get a return value when page is close: (following https://pub.dev/packages/auto_route#returning-results)

// home_screen

@RoutePage()
class HomeScreen extends StatelessWidget {
...
  final result = await context.router.pushNamed<bool>('/home/content/$index');
  if (result ?? false) {
    debugPrint('Return from /content/$index: $result');
  }
...
}

// content_screen

@RoutePage<bool>()
class ContentScreen extends StatelessWidget {
...
AppBar(
  leading: GestureDetector(
    child: const Icon(Icons.arrow_back),
    onTap: () => context.router.pop<bool>(true),
  ),
)
...
}

But I get an exception when trying to enter content page

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: type 'AutoRoutePage<dynamic>' is not a subtype of type 'AutoRoutePage<bool>' in type cast
#0      StackRouter._addEntry (package:auto_route/src/router/controller/routing_controller.dart:1552:18)
#1      StackRouter._pushAllGuarded (package:auto_route/src/router/controller/routing_controller.dart:1522:29)
<asynchronous suspension>
#2      HomeScreen.build.<anonymous closure>.<anonymous closure> (package:flutter_template_auto_route/screens/home_screen.dart:45:19)
<asynchronous suspension>

Is there a way to fix this without changing the home screen to RoutePage which is not suppose to?


flutter: 3.13.4 auto_route: 7.8.3 auto_route_generator: 7.3.1 build_runner: 2.4.6

testing on IPhoneX

Lamorak commented 10 months ago

I came across the same issue, it seems like there's a bug when using result value in nested routes. I solved mine by moving the route to the root level, but it might not be ideal for your case

fikretsengul commented 8 months ago

I have same issue I think its a bug too.

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions