Flutter + Get,toNamed()返回RouteSettings()错误

问题描述 投票:0回答:2

我对 Flutter 很陌生,所以很抱歉不理解所有术语。

我有一个从 FCM 接收数据的应用程序,它显示一个 SnackBar(使用 Get)。所有这一切都运作良好。问题是“onTap”。当我使用 Get.toNamed() 时,它会响应,

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

这是我当前的 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());
}

}

我的路线是这样设置的。

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;
    }
  }
}

我在尝试弄清楚如何导航到 onTap() 页面时陷入停滞 我也尝试过所有其他变体。任何帮助将不胜感激。谢谢!

参考:Flutter 获取包,https://pub.dev/packages/get

flutter dart flutter-dependencies dart-pub flutter-get
2个回答
0
投票

你在MaterialApp中设置了routes和onGenerateRoute吗?这是一个示例https://flutter.dev/docs/cookbook/navigation/named-routes


0
投票
  final Contact contact;

  ContactPage(this.contact, {super.key});
  final ImagePicker _picker = ImagePicker();
  XFile? _image;
  @override
  Widget build(BuildContext context) => Scaffold(
        backgroundColor: Color.fromARGB(172, 0, 0, 0),

        appBar: AppBar(
          backgroundColor: Colors.black,
          centerTitle: true,
          leading: IconButton(
            onPressed: () {
              Get.to(FlutterContactsExample);
            },
            icon: Icon(
              Icons.arrow_back,
              color: Color.fromARGB(255, 180, 180, 180),
            ),
          ),
Exception has occurred.
Unexpected format,
you can only use widgets and widget functions here ```
© www.soinside.com 2019 - 2024. All rights reserved.