Skip to content

[go_router] GoRouter's initial location ignored when getInitialRoute is overridden in Android #132402

@gyuri-fluttech

Description

@gyuri-fluttech

Is there an existing issue for this?

Steps to reproduce

  1. Create a Flutter app
  2. Provide a custom initialRoute in the native layer (Android) by overriding getInitialRoute in MainActivity.kt.
  3. Add go_router: ^10.0.0 as dependency in pubspec.yaml.
  4. Create a GoRouter instance with a different initialLocation provided. (I provided a full main.dart sample)
  5. Run the app.

Expected results

I expect to see the initial location provided in go_router.

Actual results

The initialLocation is ignored and initialRoute is used.

Code sample

Flutter main.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Flutter Demo',
      routerConfig: _defaultRouter,
      theme: ThemeData(
        // This is the theme of your application.
        //
        // TRY THIS: Try running your application with "flutter run". You'll see
        // the application has a blue toolbar. Then, without quitting the app,
        // try changing the seedColor in the colorScheme below to Colors.green
        // and then invoke "hot reload" (save your changes or press the "hot
        // reload" button in a Flutter-supported IDE, or press "r" if you used
        // the command line to start the app).
        //
        // Notice that the counter didn't reset back to zero; the application
        // state is not lost during the reload. To reset the state, use hot
        // restart instead.
        //
        // This works for code too, not just values: Most code changes can be
        // tested with just a hot reload.
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // TRY THIS: Try changing the color here to a specific color (to
        // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
        // change color while the other colors stay the same.
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          //
          // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
          // action in the IDE, or press "p" in the console), to see the
          // wireframe for each widget.
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

final GoRouter _defaultRouter = GoRouter(
  initialLocation: '/testing',
  debugLogDiagnostics: kDebugMode,
  routes: [
    GoRoute(
      path: '/testing',
      name: 'Testing',
      builder: (context, state) {
        return const MyHomePage(title: 'Testing');
      },
    ),
    GoRoute(
      path: '/home',
      name: 'Home',
      builder: (context, state) {
        return const MyHomePage(title: 'Home');
      },
    ),
  ],
);
Android MainActivity.kt
package com.example.go_route.test_go_route

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    override fun getInitialRoute(): String? {
        return "/home"
    }

}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
gyuri@Gyuri-M1 test_go_route % flutter run 
Launching lib/main.dart on SM N970F in debug mode...
Running Gradle task 'assembleDebug'...                           2,021ms
✓  Built build/app/outputs/flutter-apk/app-debug.apk.
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
Syncing files to device SM N970F...                                 36ms
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on SM N970F is available at: http://127.0.0.1:61940/rv1xSQWZ654=/
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
The Flutter DevTools debugger and profiler on SM N970F is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:61940/rv1xSQWZ654=/
I/ViewRootImpl@eec0aa2[MainActivity](17559): Relayout returned: old=(0,0,1080,2280) new=(0,0,1080,2280) req=(1080,2280)0 dur=5 res=0x3 s={true 490711089680} ch=false fn=1
I/Gralloc4(17559): mapper 4.x is not supported
I/ViewRootImpl@eec0aa2[MainActivity](17559): updateBoundsLayer: t = android.view.SurfaceControl$Transaction@ca35a77 sc = Surface(name=Bounds for - com.example.go_route.test_go_route/com.example.go_route.test_go_route.MainActivity@0)/@0x627f6e4 frame = 1
I/ViewRootImpl@eec0aa2[MainActivity](17559): mWNT: t = android.view.SurfaceControl$Transaction@ca35a77 fN = 1 android.view.ViewRootImpl.prepareSurfaces:2770 android.view.ViewRootImpl.performTraversals:4016 android.view.ViewRootImpl.doTraversal:2911 
I/ViewRootImpl@eec0aa2[MainActivity](17559): mWNT: merge t to BBQ
W/Gralloc3(17559): mapper 3.x is not supported
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/gralloc (17559): Arm Module v1.0
W/Gralloc4(17559): allocator 4.x is not supported
W/Gralloc3(17559): allocator 3.x is not supported
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] cancelDraw null isViewVisible: true
I/SurfaceView@2415db5(17559): uSP: rtp = Rect(0, 0 - 1080, 2241) rtsw = 1080 rtsh = 2241
I/SurfaceView@2415db5(17559): onSSPAndSRT: pl = 0 pt = 0 sx = 1.0 sy = 1.0
I/SurfaceView@2415db5(17559): aOrMT: uB = true t = android.view.SurfaceControl$Transaction@495c74d fN = 1 android.view.SurfaceView.access$500:124 android.view.SurfaceView$SurfaceViewPositionUpdateListener.positionChanged:1728 android.graphics.RenderNode$CompositePositionUpdateListener.positionChanged:319 
I/SurfaceView@2415db5(17559): aOrMT: vR.mWNT, vR = ViewRootImpl@eec0aa2[MainActivity]
I/ViewRootImpl@eec0aa2[MainActivity](17559): mWNT: t = android.view.SurfaceControl$Transaction@495c74d fN = 1 android.view.SurfaceView.applyOrMergeTransaction:1628 android.view.SurfaceView.access$500:124 android.view.SurfaceView$SurfaceViewPositionUpdateListener.positionChanged:1728 
I/ViewRootImpl@eec0aa2[MainActivity](17559): mWNT: merge t to BBQ
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] pdf(0) 1 android.view.ViewRootImpl.lambda$addFrameCompleteCallbackIfNeeded$3$ViewRootImpl:4987 android.view.ViewRootImpl$$ExternalSyntheticLambda16.run:6 android.os.Handler.handleCallback:938 
I/ViewRootImpl@eec0aa2[MainActivity](17559): [DP] rdf()
D/ViewRootImpl@eec0aa2[MainActivity](17559): reportDrawFinished (fn: 1) 
I/ViewRootImpl@eec0aa2[MainActivity](17559): MSG_WINDOW_FOCUS_CHANGED 1 1
D/InputMethodManager(17559): startInputInner - Id : 0
I/InputMethodManager(17559): startInputInner - mService.startInputOrWindowGainedFocus
D/InputMethodManager(17559): startInputInner - Id : 0
Application finished.

Flutter Doctor output

Doctor output
gyuri@Gyuri-M1 test_go_route % flutter doctor --verbose
[✓] Flutter (Channel stable, 3.10.6, on macOS 13.5 22G5072a darwin-arm64, locale en-RO)
    • Flutter version 3.10.6 on channel stable at /Users/gyuri/Tools/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f468f3366c (4 weeks ago), 2023-07-12 15:19:05 -0700
    • Engine revision cdbeda788a
    • Dart version 3.0.6
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/gyuri/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • 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 17.0.6+0-17.0.6b829.9-10027231)

[✓] IntelliJ IDEA Community Edition (version 2022.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • 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

[✓] VS Code (version 1.81.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)
    • SM N970F (mobile) • R58M903CJ3Z • android-arm64  • Android 12 (API 31)
    • macOS (desktop)   • macos       • darwin-arm64   • macOS 13.5 22G5072a darwin-arm64
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 115.0.5790.170

[✓] Network resources
    • All expected network resources are available.

• No issues found!
gyuri@Gyuri-M1 test_go_route % 

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectfound in release: 3.10Found to occur in 3.10found in release: 3.14Found to occur in 3.14has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions