modal_bottom_sheet icon indicating copy to clipboard operation
modal_bottom_sheet copied to clipboard

BottomSheetRoute throws if declared as initial route

Open tomwyr opened this issue 2 years ago • 2 comments

I am using modal_bottom_sheet together with auto_route package. My routes declaration looks more or less like this:

@MaterialAutoRouter(
  routes: [
    AutoRoute(
      path: '/',
      page: EmptyRouterPage,
      name: 'RootRoute',
      children: [
        AutoRoute(
          initial: true,
          page: HomePage,
        ),
        CustomRoute(
          page: EmptyRouterPage,
          name: 'OtherFlowRoute',
          children: [
            CustomRoute(
              initial: true,
              page: EmptyRouterPage,
              name: 'BottomSheetRoute',
              customRouteBuilder: buildModalBottomSheetRoute,
              children: [
                // ... bottom sheet routes
              ],
            ),
            // ... other flow routes
          ],
        ),
      ],
    ),
  ],
)

When I try to navigate to other flow and push modal bottom sheet as the initial route, I get following error:

════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building WillPopScope(dependencies: [_ModalScopeStatus], state: _WillPopScopeState#2ab01):
Null check operator used on a null value

The relevant error-causing widget was
AutoRouter
lib/widgets.dart:36
When the exception was thrown, this was the stack
#0      ModalBottomSheetRoute.createAnimationController
package:modal_bottom_sheet/src/bottom_sheet_route.dart:176
#1      TransitionRoute.install
package:flutter/…/widgets/routes.dart:220
#2      ModalRoute.install
package:flutter/…/widgets/routes.dart:1146
#3      _RouteEntry.handleAdd
package:flutter/…/widgets/navigator.dart:2818
#4      NavigatorState._flushHistoryUpdates
package:flutter/…/widgets/navigator.dart:3813
#5      NavigatorState.restoreState
package:flutter/…/widgets/navigator.dart:3318
#6      RestorationMixin._doRestore
package:flutter/…/widgets/restoration.dart:887
#7      RestorationMixin.didChangeDependencies
package:flutter/…/widgets/restoration.dart:873
#8      NavigatorState.didChangeDependencies
package:flutter/…/widgets/navigator.dart:3336
#9      StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4914
#10     ComponentElement.mount
package:flutter/…/widgets/framework.dart:4729
// more stack trace

This is because ModalBottomSheet attempts to create animation controller before first frame in which case overlay is still unavailable. https://github.com/jamesblasco/modal_bottom_sheet/blob/master/lib/src/bottom_sheet_route.dart#L176 Is there any specific reason to use overlay as TickerProvider? E.g. bottom sheet from the SDK simply uses navigator to create the controller: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/bottom_sheet.dart#L513

Let me know if I could provide any additional info that would be helpful.

tomwyr avatar Mar 21 '22 11:03 tomwyr

Any update on this? In my case, this was happened when my app was restoring state and the bottom sheet was in the top of the navigation stack.

mrhabibi avatar May 23 '22 02:05 mrhabibi

Same issue. Any update on this?

guenth39 avatar Sep 20 '22 15:09 guenth39

Same issue here unfortunately

janknips avatar Jan 10 '23 17:01 janknips

Having this issue as well.

pgulegin avatar Jan 19 '23 04:01 pgulegin

Same issue.

taiseidev avatar Jan 24 '23 09:01 taiseidev

Same issue here

talhakerpicci avatar Feb 19 '23 10:02 talhakerpicci

Hi. The issue is on this line https://github.com/jamesblasco/modal_bottom_sheet/blob/39174843294cf19ec8a3a624c32034227cdfd788/modal_bottom_sheet/lib/src/bottom_sheet_route.dart#L183

just put navigator! instead navigator!.overlay and it should work

vasilich6107 avatar Aug 08 '23 13:08 vasilich6107

It looks like that's what they do in Flutter ModalBottomSheetRoute.

  @override
  AnimationController createAnimationController() {
    assert(_animationController == null);
    if (transitionAnimationController != null) {
      _animationController = transitionAnimationController;
      willDisposeAnimationController = false;
    } else {
      _animationController = BottomSheet.createAnimationController(navigator!);
    }
    return _animationController!;
  }

I opened a PR with that change: https://github.com/jamesblasco/modal_bottom_sheet/pull/379

kvenn avatar Nov 07 '23 23:11 kvenn