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

Bad state: No element on Browser reload #655

Open Terranic opened 10 months ago

Terranic commented 10 months ago

Describe the bug Bad State: no element exception after web app reload (starting it works smoothly)

Beamer version: 1.6.0 (and 2.0.0-dev.1)

To Reproduce Steps to reproduce the behavior:

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

    @override
    Widget build(BuildContext context) {
      final routerDelegate = BeamerDelegate(
        locationBuilder: RoutesLocationBuilder(
          routes: {
            '/': (context, state, data) => const MainWidget(""),
            '/bot/:botId': (context, state, data) {
              final botId = state.pathParameters['botId']!;
              return MainWidget(botId);
            }
          },
        ).call,
      );
      return MaterialApp.router(
        routeInformationParser: BeamerParser(),
        routerDelegate: routerDelegate,
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
      );
    }
  }

For this code, I get the following exception - but only when reloading the web app

When the exception was thrown, this was the stack: 
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3  throw_
dart-sdk/lib/_internal/js_dev_runtime/private/js_array.dart 356:5            last]
packages/beamer/src/beamer_delegate.dart 975:35                              [_setBrowserTitle]
packages/beamer/src/beamer_delegate.dart 760:9                               <fn>
packages/flutter/src/widgets/basic.dart 7682:48                              build
packages/flutter/src/widgets/framework.dart 5541:22                          build
...

which is this code

  void _setBrowserTitle(BuildContext context) {
    if (active && kIsWeb && setBrowserTabTitle) {
      SystemChrome.setApplicationSwitcherDescription(
          ApplicationSwitcherDescription(
EXC-->        label: _currentPages.last.title ??
            currentBeamLocation.state.routeInformation.uri.path,
        primaryColor: Theme.of(context).primaryColor.value,
      ));
    }
  }

Expected behavior No Exception :-)

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

lenzpaul commented 9 months ago

@Terranic Try declaring your BeamerDelegate outside of the build method.

stan-at-work commented 4 months ago

It’s highly likely that this is the issue. Make sure to place your Beamer declaration in the initState method or in your main.dart file, but definitely not in a build function.

stan-at-work commented 4 months ago

@Terranic Is this still an issue?