Fetch Arguments In Flutter Module
I have a native Android app. I imported a Flutter module. Now, I can successfully navigate to the selected route from my Android app. I know passing data between native and flutter
Solution 1:
Now, I found an approach. I'm sure there are better ways than this. First, I will override the onCreate
method of MyFlutterActivity
class. Then set the parameter in a global variable.
String title = "";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
title = extras.getString("title");
}
@OverridepublicvoidconfigureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
newMethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(((methodCall, result) -> {
if (methodCall.method.equals("getBatteryLevel")) {
result.success("batteryLevel"); // It returns string "batteryLevel".
} elseif (methodCall.method.equals("getTitle")) {
result.success(title); // It returns string "Hello".
} else {
result.notImplemented();
}
}));
}
Now, I can invoke the method in my initState
of SecondScreen
to get the title that passed completely from the native side.
var title = await platformMethodChannel.invokeMethod('getTitle');
Post a Comment for "Fetch Arguments In Flutter Module"