slovnicki / beamer

A routing package built on top of Router and Navigator's pages API, supporting arbitrary nested navigation, guards and more.
MIT License
591 stars 129 forks source link

Last widget/page is being rebuilt when navigating to different beam location, which results in errors #661

Closed Sten435 closed 6 months ago

Sten435 commented 8 months ago

Describe the bug When navigation to a different beam location, the current page you are on (the location where you are navigating away from) is re-build 1 more time.

Since I put my Bloc Providers in each beam location and I navigate to location B from location A and the current page of location A is rebuilt right before navigating (for some reason this happens). I get a (bloc) provider not found exception.

Beamer version: beamer: ^1.6.1

To Reproduce

To temp fix this is my code, I do a if(context.watch<Foo?> == null) return SizedBox.shrink();.

Expected behavior Navigate from beam location A to beam location B without rebuilding the beamPage 'homePage' after beamToNamed is called.

Desktop (please complete the following information):

Sten435 commented 6 months ago

@slovnicki any updates ?

Sten435 commented 6 months ago

Found a solution.

Do not register, any providers in the builder(...) function in the location.

Instead if you want to wrap every page with a provider,

Extend beampage and wrap your providers arround the child proppety.

This works but is not pretty.

It should be fixed in beamer itself.

stan-at-work commented 4 months ago

It also seems to help to put the transitionDelegates to: NoAnimationTransitionDelegate()

For example:

  static Future<void> ensureInitialized() async {
    final appOptions = CoreAppOptions.instance;

    delegate = BeamerDelegate(
      initialPath: appOptions.initialPathOfApp,
      stackBuilder: (routeInformation, beamParameters) {
        CoreAppBeamerDelegate.beamParameters = beamParameters;
        CoreAppBeamerDelegate.routeInformation = routeInformation;

        return BeamerStackBuilder(
          beamStacks: [
            CoreAuthLocation(),
            CoreEnvironmentLocation(),
            CoreProfileLocation(),
            CoreSettingsLocation(),
            ...appOptions.locations(),
          ],
        ).call(routeInformation, beamParameters);
      },
      routeListener: (routeInformation, beamerDelegate) {
        if (kDebugMode) {
          debugPrint('Navigate to ---> ${routeInformation.uri}');
        }
      },
// HERE
      beamBackTransitionDelegate: const CoreNoAnimationTransitionDelegate(),
      transitionDelegate: const CoreNoAnimationTransitionDelegate()
// HERE,
      notFoundPage: const BeamPage(type: BeamPageType.noTransition, child: CoreNotFoundScreen(), key: ValueKey('tools.notFoundPage.NotFound')),
    );
  }