Flutter + Get, toNamed() returns RouteSettings() error

Issue

I am very new to Flutter so I apologize for not understanding all the terminology.

I have an application that receives data from FCM, and it shows a SnackBar (using Get). All of this is working well. The problem is the ‘onTap’. When I use Get.toNamed(), it responds with,

Could not find a generator for route RouteSettings("/home-screen", null) in the _WidgetsAppState.

This is my current Snackbar

void showDialog(BuildContext context, messageJson) {
print('showDialog');
try {
  final data = messageJson['data'];
  final notification =
  data != null && data.keys.isNotEmpty ? data : messageJson['notification'];
  var body = notification['body'];
  var title = notification['title'];

  Get.snackbar(
    title,
    body,
    icon: Icon(Icons.chat),
    shouldIconPulse: true,
    onTap: (index) {
      Get.toNamed('/home-screen');
      //Navigator.of(context).pushNamed('/home-screen'); // <-- Didn't work either
    },
    isDismissible: true,
    duration: Duration(seconds: 4),
  );

} catch (e) {
  print(e.toString());
}

}

With my Routes being setup like this.

class Routes {
  static final Map<String, WidgetBuilder> _routes = {
    "/home-screen": (context) => HomeScreen(),
    "/home": (context) => MainTabs(),
    "/login": (context) => LoginScreen(),
    "/register": (context) => RegistrationScreen(),
    ...VendorRoute.getAll(),
  };
  static Map<String, WidgetBuilder> getAll() => _routes;

  static WidgetBuilder getRouteByName(String name) {
    if (_routes.containsKey(name) == false) {
      return _routes[RouteList.homeScreen];
    }
    return _routes[name];
  }

  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case RouteList.storeDetail:
        return MaterialPageRoute(
          builder: VendorRoute.getRoutesWithSettings(settings)[settings.name],
        );
      default:
        return null;
    }
  }
}

I’m at a standstill on trying to figure out how to navigate to a page onTap()
I have tried all the other variations as well. Any help would be appreciated here. Thanks!

REF: Flutter Get Package, https://pub.dev/packages/get

Solution

Did you set routes and onGenerateRoute inside MaterialApp? Here is an example https://flutter.dev/docs/cookbook/navigation/named-routes

Answered By – Joey Wong

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *