Skip to content

Instantly share code, notes, and snippets.

@callmephil
Created January 10, 2024 16:15
Show Gist options
  • Save callmephil/d5c02c84a788fce17b682db0864074ff to your computer and use it in GitHub Desktop.
Save callmephil/d5c02c84a788fce17b682db0864074ff to your computer and use it in GitHub Desktop.
go router deeplink bug
import 'package:flutter/material.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:go_router/go_router.dart';
final _navKey = GlobalKey<NavigatorState>();
void main() {
usePathUrlStrategy();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
routerConfig: GoRouter(
navigatorKey: _navKey,
debugLogDiagnostics: true,
routerNeglect: true,
initialLocation: '/',
routes: [
GoRoute(
path: '/',
builder: (context, state) {
return const ParentScreen();
},
routes: [
GoRoute(
path: ':id',
builder: (context, state) {
return const ChildScreen();
},
),
],
),
],
),
);
}
}
class ParentScreen extends StatefulWidget {
const ParentScreen({super.key});
@override
State<ParentScreen> createState() => _ParentScreenState();
}
class _ParentScreenState extends State<ParentScreen> {
@override
Widget build(BuildContext context) {
// This print statement is being called if the child screen is opened.
print('parent screen has been built');
return Scaffold(
appBar: AppBar(
title: const Text('Parent Screen'),
),
body: Center(
child: TextButton(
onPressed: () => context.go('/1'),
child: const Text('Go to child screen'),
),
),
);
}
}
class ChildScreen extends StatelessWidget {
const ChildScreen({super.key});
@override
Widget build(BuildContext context) {
print('child screen has been built');
return Scaffold(
appBar: AppBar(
title: const Text('Child Screen'),
),
body: Center(
child: TextButton(
onPressed: () => context.go('/'),
child: const Text('Go to parent screen'),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment