auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

Unhandled type Exception for RoutePage

Open ChivialK opened this issue 2 years ago • 3 comments

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

ChivialK avatar Sep 14 '23 06:09 ChivialK

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

Lamorak avatar Dec 11 '23 10:12 Lamorak

I have same issue I think its a bug too.

fikretsengul avatar Feb 05 '24 13:02 fikretsengul