我如何通过意图将我的本机代码中的参数传递给 flutter?

How can i pass arguments from my native code to flutter via intent?

我正在使用 Intent 在 flutter 中启动特定路线,如下所示

class NotificationService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if(remoteMessage.data.isNotEmpty()) {
            val intent = Intent(this, MainActivity::class.java)
            intent.putExtra("route","/specific_route_name");
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(intent);
        }
    }
}

我的 onGenerated 函数负责处理和启动如下所示的抖动屏幕

static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case '/':
        return CupertinoPageRoute(builder: (_) => MyHomePage());
      case '/specific_route_name':
        return CupertinoPageRoute(builder: (_) => SpecificPage());
      default:
        return CupertinoPageRoute(
          builder: (_) => Scaffold(
            body: Center(
              child: Text('No route defined for ${settings.name}'),
            ),
          ),
        );
    }
  }

现在我怎样才能传递数据呢?通过这个意图,我尝试了 intent.putExtra("data") & intent.putExtra("argument") (刚刚尝试了我的运气不好)

嗯..你不需要(需要)。只需将所有内容编码到根路径中。例如 /specific_route_name?foo=bar 然后使用类似:

    final uri = Uri.parse(settings.name);
    final route = uri.path;
    final param = uri.queryParameters['foo'];

这样你的路由名称就会在route中,并且可以通过uri.queryParameters读出你的参数。