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

202

Well.. you don't (need to). Simply encode everything into the root path.. like /specific_route_name?foo=bar and then use something like:

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

this way you will have your route name in route and can read out your parameters through uri.queryParameters.

Share:
202
Gurkarn Singh
Author by

Gurkarn Singh

Updated on December 31, 2022

Comments

  • Gurkarn Singh
    Gurkarn Singh over 1 year

    i am launching a specific route in flutter using Intent like below

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

    and my onGenerated function is responsible to handle and launch that flutter screen like below

    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}'),
                ),
              ),
            );
        }
      }
    

    now how can i pass data too? via that intent i tried intent.putExtra("data") & intent.putExtra("argument") (just tried my dumb luck)

  • Gurkarn Singh
    Gurkarn Singh almost 3 years
    Thanks for answering, but yea i know that already; i kept this as my last choice, actually i need to pass a object which i can obviously stringify and pass as a query param. but i really wanted to know that if passing data using intent is possible
  • Herbert Poul
    Herbert Poul almost 3 years
    no.. not out of the box. But you can write a plugin which simply returns the Intent data.
  • Gurkarn Singh
    Gurkarn Singh almost 3 years
    huh! i think you're right, the amount of research i've done makes me believe that i have to come up with third party solution, thanks btw!