Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Navigating programmatically to a parent route from a sub-route of type NoTransition throws an Exception #205

Closed
osaxma opened this issue Dec 2, 2021 · 2 comments

Comments

@osaxma
Copy link

osaxma commented Dec 2, 2021

It's a coincidence that NoTransition was just introduced and yesterday I was struggling to debug the following error that I later found out it was caused by using transitionDuration: Duration.zero in certain sub-routes.

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building InheritedGoRouter(goRouter: Instance of 'GoRouter'):
Assertion failed:                                                      ../…/widgets/navigator.dart:3955
_debugLocked && !_debugUpdatingPage
is not true

The relevant error-causing widget was
InheritedGoRouter                                                  ../…/src/go_router.dart:48

To Reproduce:

  • Run the example below
  • Navigate to profile
  • then navigate to home
  • the error should appear.
// ignore_for_file: prefer_expression_function_bodies, prefer_const_constructors, avoid_print,  lines_longer_than_80_chars
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  App({Key? key}) : super(key: key);

  static const title = 'No Transition bug';

  @override
  Widget build(BuildContext context) => MaterialApp.router(
        routeInformationParser: _router.routeInformationParser,
        routerDelegate: _router.routerDelegate,
        title: title,
      );

  late final _router = GoRouter(
    routes: [
      GoRoute(
        path: '/',
        builder: (context, state) => const Home(),
        routes: [
          GoRoute(
            path: 'profile',
            pageBuilder: (context, state) => NoTransitionPage(child: const Profile()),
          ),
        ],
      ),
    ],
  );
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          child: Text('Go Profile'),
          onPressed: () => context.go('/profile'),
        ),
      ),
    );
  }
}

class Profile extends StatelessWidget {
  const Profile({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          child: Text('Go Home'),
          onPressed: () => context.go('/'),
        ),
      ),
    );
  }
}

Additional Context:
The error will not occur if profile was a top level route. So it seems the error would occur when a sub-route of type NoTransition is navigating programmatically to its parent route.

When I was debugging, the error occurred in all of iOS, macOS, and Web using Flutter: (Channel stable, 2.5.3, on macOS 11.6 20G165 darwin-x64)

Work Around
A simple fix for me was to place the smallest transition duration possible and apparently a 1-µs does the trick.

GoRoute(
  path: 'profile',
  pageBuilder: (context, state) => CustomTransitionPage(
    transitionsBuilder: (context, animation, secondaryAnimation, child) => child,
    transitionDuration: const Duration(microseconds: 1),
    child: const Profile(),
  ),
),

Though I'm not sure if there's something can be done on the go_router side to make Durration.zero work. When researching, the error seems to be common with Navigator 1.0 and the Scaffold Drawer where most solutions were adding an arbitrary Future.delay or postframeCallbacks.

@osaxma osaxma changed the title NoTransition can have unintended consequences Navigating programmatically to a parent route from a sub-route of type NoTransition throws an Exception Dec 2, 2021
csells added a commit that referenced this issue Dec 3, 2021
@csells
Copy link
Owner

csells commented Dec 3, 2021

@osaxma good catch! I published a fix in v2.5.1. Can you verify?

@osaxma
Copy link
Author

osaxma commented Dec 3, 2021

Yes, the error doesn't show anymore.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants