Closed bw-flagship closed 2 years ago
@bw-flagship this sounds like a bug. is this still an issue for you?
@csells Yes it is. it is reproducable it with the provided sample and go_router 3.0.1
Yep, encountered the same issue. Trying to push through multiple levels at once, but doesn't work. And yes, indeed, looks like, after the 1st route, the state loses params for some reason (although they are in the uri).
I have encounter the same issue in my project. Furthermore I have created a minimal code to reproduce this behavior :
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => Screen(title: "Home - ${state.location}"),
routes: [
GoRoute(
path: 'family/:fid',
builder: (context, state) => Screen(title: "Family ${state.params['fid']} - ${state.location}"),
routes: [
GoRoute(
path: 'person/:pid',
builder: (context, state) => Screen(title: "Family ${state.params['fid']} - Person ${state.params['pid']} - ${state.location}"),
),
],
),
],
),
],
);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'GoRouter',
routeInformationParser: _router.routeInformationParser,
routerDelegate: _router.routerDelegate,
);
}
}
class Screen extends StatelessWidget {
final String title;
const Screen({Key? key, required this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
button(context, "Family 3", '/family/3'),
button(context, "Family 3 person 2", '/family/3/person/2'),
],
),
),
);
}
Widget button(BuildContext context, String label, String path) {
void Function() onPressed = () => context.push(path); // Here the [:fid] is null when push nested route
// void Function() onPressed = () => context.go(path); // Here the [:fid] is retrive correctly
return ElevatedButton(onPressed: onPressed, child: Text(label));
}
}
WORKAROUND: We can easily parse the state.location to retrieve the :fid inside the builder function of the GoRoute
// To search number ID
String? getIDInsideURI(String url, String label) => RegExp(r'\/' + label + r'\/\d*').stringMatch(url)?.split("/").last;
// To search uuid ID
String? getUUIDInsideURI(String url, String label) => RegExp(r'\/' + label + r'\/[\d\w-]*').stringMatch(url)?.split("/").last;
// -----------------
GoRoute(
path: 'person/:pid',
builder: (context, state) {
String fid = getIDInsideURI(state.location, "family")!;
return Screen(title: "Family $fid - Person ${state.params['pid']} - ${state.location}");
}
),
Scenario: We have two nested routes (families + persons) and a third, separate route (home). When I am on home and then use context.go to reach a specific person, it works. But when I use context.push with the same argument, it does not work because the first parameter (family id) is not available. A workaround is to push the families screen first and the persons-screen afterwards.
To provide a reproducible sample, I copied the "GoRouter Example: Nested Navigation" and modified it accordingly. When you run the code below, you find three buttons to demonstrate the issue.
If I can assist with any further information, do not hesitate to ask.
Thanks!