'Using onGenerateRoute with restorationScope

I'm trying to create an app in Flutter that uses onGenerateRoute while, at the same time, using the restoration feature. If I use a simple map of routes and widgets I get no errors, but if I try to use onGenerateRoute and a RouteFactory I get a "_history.isNotEmpty" error on the Navigator.

I'm testing it by selecting "Don't keep activities" on a Android Emulator. The app runs at first, but even in the first screen (root/initRoute) the following error occurs when coming back to the app after losing focus.

Note: I'm using enhanced enums here, but my error comes directly from MaterialApp widget, not even entering my "buildRoutes" static method.

My code looks something like this:

child: MaterialApp(
  restorationScopeId: 'root_app',
  onGenerateRoute: Routes.buildRoutes(), //This doesn't work
  /*routes: {                            //This does work!
    '/': (context) => const Menu(),
    '/selection': (context) => const Selection(),
  }*/,
  title: 'My App',
  theme: ThemeData(
    primarySwatch: Colors.teal,
  ),
),

My Routes.buildRoutes is as follows:

enum Routes {
  menu('/', 'menu'),
  selection('/selection', 'selection');

  final String value;
  final String restorationId;
  const Routes(this.value, this.restorationId);

  static RouteFactory buildRoutes() {
    return (settings) {
      Widget _view;

      var route = findRoute(settings.name);
      switch (route) {
        case Routes.menu:
          _view = const Menu();
          break;
        case Routes.selection:
          _view = const Selection();
          break;
        default:
          return null;
      }

      return MaterialPageRoute(
        builder: (context) => RestorationScope(restorationId: route.restorationId, child: _view),
      );
    };
  }
}

My app actually runs without problems but, for some bizarre reason everytime I try to restore my app, I get the following error:

Debug Console Error Image App Screen Error Image



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source