Open mdrideout opened 2 years ago
Hey @tomgilder - curious if your gut reaction to this is that I have some sort of anti-pattern happening somewhere? (on 0.10.0-dev5)
Having implemented this solution to be able to navigate, I now get the following error with this solution:
[debug] Capture from onError 'package:routemaster/src/route_data.dart': Failed assertion: line 194 pos 12: 'routeData != null': Couldn't find RouteData for page
[error] Exception caught by widgets library
The offending call is to this RouteData which is in a widget on the screen (not the drawer in the example below).
// Get this route's path
String path = RouteData.of(context).path;
The navigation still works fine. But navigating away from the screen using the drawer is what triggers the above error.
Example drawer navigation feature
class MainDrawer extends ConsumerWidget {
const MainDrawer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, ScopedReader watch) {
// Vars
User? _user = context.read(userStateProvider).user;
String? _name = _user?.parsedIdToken.name;
String? _email = _user?.email;
UserType? _userType = _user?.userType;
String? _userSupportUrl = _user?.accountData.supportUrl;
String? _userSupportEmail = _user?.accountData.supportEmail;
/// Close Drawer & Maybe Navigate
void navigateOrClose(String path) {
if (RouteData.of(context).path == path) {
// We're already there, close the drawer
Navigator.pop(context);
} else {
// Close the drawer, then navigate
Navigator.pop(context);
Routemaster.of(context).push(path);
}
}
/// Render drawer
return Drawer(
child: Column(
OK so i have another solution for if you encounter the above Failed assertion
error. I looked a bit more through the code and found the maybeOf
variant to get RouteData, which allows it to return null if no routedata is found.
So I've changed the code as follows to eliminate the error.
String path = RouteData.of(context).path;
to
String? _path = RouteData.maybeOf(context)?.path;
Deep Nested Path Example:
/products/:productId/details
Another Template:/profile
Example Flow:
Routemaster.of(context).push('/products')
Routemaster.of(context).push('/products/:productId/details')
Drawer()
overlay widget:Routemaster.of(context).push('/profile')
Attempt at Step 3 results in no page transition, however, the URL path does change to match the desired path. Opening the drawer and clicking the same route button again will result in the following errors. (Note: this does not occur with buttons in the normal screen widget, only in the Drawer class)
Web Error
iOS Error (provides more data re: routemaster)
Required Solution
The user must first close the drawer, before calling the Routemaster push method. Note, this does not happen with transitioning between two "Root" paths (both starting with a '/' and having only the single path segment).
Example function that closes the drawer if we're already on the path, or first closes the drawer and then navigates.