Skip to content

[go_router] Cannot go to the target page when using redirect #142393

@firgia

Description

@firgia

Steps to reproduce

  1. Use the code sample.
  2. Run flutter run on iOS simulator.
  3. Tap the edit profile button
  4. While the keyboard shows, swipe (left to right) QUICKLY to back to the previous page.
  5. Tap the edit profile button

FYI:
I have 2 screens (home and edit profile page). But on the tablet I merged 2 pages into 1 screen, so I don't need the edit profile route on tablet devices.

When I used context on redirect it's called back the redirect in multiple times when I showed or dismissed the soft keyboard.

The main issue is I can't go to the edit profile page after swiping back quickly when the keyboard still appears. This issue doesn't appear when swiping slowly or removing context on redirect functions.

Please check the video below, Thanks.

Expected results

Should go to the edit profile page

Actual results

Cannot go to the edit profile page (even I get going to /home/edit-profile log after tap the edit profile button)

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

extension ScreenDeviceExtension on BuildContext {
  bool get isTablet {
    BuildContext context = this;

    return MediaQuery.of(context).size.width >= 650;
  }
}

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  App({super.key});

  final GoRouter router = GoRouter(
    initialLocation: "/home",
    routes: [
      GoRoute(
        path: "/home",
        name: "home",
        builder: (context, state) => const HomePage(),
        routes: [
          GoRoute(
            path: "edit-profile",
            name: "edit-profile",
            builder: (context, state) => const EditProfilePage(),
          ),
        ],
      ),
    ],
    redirect: (context, state) {
      // Do not go to the edit profile page on Tablet, because the edit profile
      // page is already merged in Home screen
      if (context.isTablet) {
        String? fullPath = state.fullPath;

        if (fullPath?.contains("edit-profile") ?? false) {
          fullPath!.replaceAll("edit-profile", "");
        }

        return fullPath;
      } else {
        return null;
      }
    },
    debugLogDiagnostics: true,
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: router,
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Home")),
      body: context.isTablet
          ? const Center(
              child: Row(
              children: [
                Expanded(child: Text("Home content")),
                VerticalDivider(width: 50, thickness: 1),
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(hintText: "Username"),
                  ),
                ),
              ],
            ))
          : Center(
              child: ElevatedButton(
                onPressed: () => context.goNamed("edit-profile"),
                child: const Text("Edit profile"),
              ),
            ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Edit profile")),
      body: const Center(
        child: TextField(
          autofocus: true,
          decoration: InputDecoration(hintText: "Username"),
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
Simulator.Screen.Recording.-.iPhone.12.-.2024-01-28.at.12.45.44.mp4

Logs

Logs
[GoRouter] Full paths for routes:
             => /home
             =>   /home/edit-profile
           known full paths for route names:
             home => /home
             edit-profile => /home/edit-profile
[GoRouter] setting initial location /home
[GoRouter] Using MaterialApp configuration
[GoRouter] getting location for name: "edit-profile"
[GoRouter] going to /home/edit-profile
[GoRouter] getting location for name: "edit-profile"
[GoRouter] going to /home/edit-profile

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.16.5, on macOS 14.2.1 23C71 darwin-arm64, locale en-ID)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.85.2)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listfound in release: 3.16Found to occur in 3.16found in release: 3.19Found to occur in 3.19has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.r: 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