flutter / flutter/flutter

[go_router] StatefulWidget not called initState when context.go on second time?

Open
#168,957 9 comments 1 reaction 0 assignees View on GitHub
p: go_router P2 package team-ecosystem triaged-ecosystem
Dominant language
Dart
Stars
179k
Forks
31.1k
PR merge metrics
PR metrics pending

Description

### Steps to reproduce

1. App launched → AScreen
2. Push to → BScreen
3. Push to → CScreen
4. Go to BScreen → `initState` called ✅
5. Push to → CScreen
6. Go to BScreen → `initState` not called ❌

### Expected results

`initState` method of `BScreen` called on second time when using `context.go`.

### Actual results

`initState` method of `BScreen` not called on second time when using `context.go`.

### Code sample

Code sample

```dart
class MyApp extends StatelessWidget {
MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp.router(routerConfig: _router);
}

final _router = GoRouter(
debugLogDiagnostics: true,
initialLocation: '/A',
routes: [
StatefulShellRoute.indexedStack(
builder:
(context, state, navigationShell) =>
MainHomeScreen(navigationShell: navigationShell),
branches: [
StatefulShellBranch(
routes: [
GoRoute(path: '/A', builder: (context, state) => AScreen()),
GoRoute(
path: '/B',
builder:
(context, state) => BScreen(arg: state.extra as BArgument),
),
GoRoute(path: '/C', builder: (context, state) => CScreen()),
],
),
],
),
],
);
}

class AScreen extends StatelessWidget {
const AScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('A screen')),
floatingActionButton: FloatingActionButton(
onPressed:
() => context.push('/B', extra: BArgument('From A', 'Desc From A')),
),
);
}
}

class BScreen extends ConsumerStatefulWidget {
final BArgument arg;

const BScreen({super.key, required this.arg});

@override
ConsumerState createState() => _BScreenState();
}

class _BScreenState extends ConsumerState {
@override
void initState() {
log('>>>>>>>>>initState BScreen ${widget.arg.text}');
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('B screen')),
floatingActionButton: FloatingActionButton(
onPressed: () => context.push('/C'),
),
);
}
}

class CScreen extends StatelessWidget {
const CScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('C screen')),
floatingActionButton: FloatingActionButton(
onPressed:
() => context.go('/B', extra: BArgument('From C', 'Desc from C')),
),
);
}
}

class BArgument {
final String text;
final String desc;

BArgument(this.text, this.desc);
}
```

### Flutter Doctor output

Doctor output

```console
[✓] Flutter (Channel stable, 3.29.1, on macOS 15.3.1 24D70 darwin-arm64, locale en-VN) [3.3s]
• Flutter version 3.29.1 on channel stable at /Volumes/Untitled/Workspace/fvm/versions/3.29.1
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 09de023485 (3 months ago), 2025-02-28 13:44:05 -0800
• Engine revision 871f65ac1b
• Dart version 3.7.0
• DevTools version 2.42.2

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [4.0s]
• Android SDK at /Users/nta-trongpq/Library/Android/sdk
• Platform android-35, build-tools 35.0.0
• ANDROID_HOME = /Users/user/Library/Android/sdk
• Java binary at: /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java
This JDK is specified in your Flutter configuration.
To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
• Java version Java(TM) SE Runtime Environment (build 17.0.11+7-LTS-207)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2) [4.3s]
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 16C5032a
• CocoaPods version 1.16.2

[✓] Android Studio (version 2024.3) [13ms]
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 21.0.5+-13047016-b750.29)

[✓] VS Code (version 1.100.1) [9ms]
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.110.0

[✓] Connected device (4 available) [8.2s]
• sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 14 (API 34) (emulator)
• iPhone 13 Pro Max (wireless) (mobile) • 00008110-00084C683CFA801E • ios • iOS 18.4 22E239
• macOS (desktop) • macos • darwin-arm64 • macOS 15.3.1 24D70 darwin-arm64
• Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 15.3.1 24D70 darwin-arm64
! Error: Browsing on the local area network for Nitrotech Asia’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources [1,164ms]
• All expected network resources are available.

• No issues found!

```

Sorry for mention, @chunhtai
Thank you!

Contributor guide

Open the contributing guide

Research direction

Start with the provided minimal Flutter example using StatefulShellRoute.indexedStack and reproduce the A → B → C navigation sequence with context.push and context.go. Inspect the route and widget lifecycle behavior around the BScreen initState log; done means the observed lifecycle behavior is documented or corrected and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.