Pass arguments to flutter activity from java - android

I have an android native application with a flutter module.
I need to open an activity from java and send some arguments to flutter. Here is my code where I call my flutter "activity":
startActivity(
FlutterActivity
.withCachedEngine(flutterNameEngine)
.build(this)
);
I can do this:
..build(this).putExtra("arg_1","Hello world")
But I don't know how to receive the data on my flutter / dart code. Any idea?

Related

Start android project activity from integrated flutter module?

I have an android app, I integrated a flutter module into it based on https://docs.flutter.dev/development/add-to-app/android/project-setup.
I managed to get calling flutter activity working, even sending extra data with intent and getting response data from flutter too.
What I want to do is the equivalent of calling startActivity(intent), just from my flutter module.
How could I achieve this? Is my only way is deeplinking?
I managed to make this work by using the package "android_intent_plus". From there, all I had to do was use this code:
var intent = AndroidIntent(
package: "my.custom.package",
componentName: "my.custom.package.MyAndroidActivity",
arguments: {
"DATA_KEY": "Data to send",
},
);
await intent.launch();
However, this solution is not perfect as of date, because I cannot receive result this way.

Pass data to Flutter View Controller

I have added flutter to a native iOS code. I have UIButton and on pressing this button, I am presenting a FlutterViewController. I have been following this.
Here's my code for presenting the FlutterViewController:
#objc
#IBAction func openProfile() {
print("open profile")
lazy var flutterEngine = FlutterEngine(name: "patient_profile_engine")
flutterEngine.run(withEntrypoint: "", libraryURI: "");
let profileVC =
FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
present(profileVC, animated: true, completion: nil)
}
This code is working fine and the flutter view is opening and since I haven't specified and entry point yet so it using the main.dart.
The problem is that I need to pass some information to the Flutter dart code. e.g. a dictionary ["patient_id": "123456"] during the initialization.
I will have to do the same in Android native Java code as well.
Is there any easy way?
You will probably want to write a platform channel to communicate between the native code and the flutter engine.
An alternative, for this purpose, would be to use runWithEntrypoint:initialRoute: method to start the engine, encode the data into the initial route, and parse it on the flutter side.

How do I pass arguments to Flutter/Dart main function when I create engine in Android/iOS?

I want pass some arguments to Flutter main function in main.dart.
const customTestFlag = bool.fromEnvironment("CUSTOM_TEST_FLAG");
void main(List<String> args) {
// I want get some arguments in there
print('get args from main.dart => ${args.toString()}');
// or get arguments form environment
print('get args form env => $customTestFlag');
runApp(app.MyApp());
}
And I create flutter engine in Android like these:
List<String> dartVmArgs = new ArrayList<>();
dartVmArgs.add("--CUSTOM_TEST_FLAG=true");
dartVmArgs.add("--dart-define=CUSTOM_TEST_FLAG=true");
dartVmArgs.add("--dart-entrypoint-args=CUSTOM_TEST_FLAG=true");
dartVmArgs.add("--dart-entrypoint-args CUSTOM_TEST_FLAG=true");
FlutterEngine flutterEngine = new FlutterEngine(app, (String[]) dartVmArgs.toArray());
But these code not working for me,maybe launch app like flutter run --dart-define="CUSTOM_TEST_FLAG=true" will work but this does not meet our needs, we expect to pass the value when Flutter engine is created or started
Thank you.
Create a global variable with const keyword.
const test = String.fromEnvironment('test', defaultValue='defaultValue');
Then in main.dart file you can directly access test variable easily.
While running app please use
flutter run --dart-define=test=yourDataHere
For more please refer to this link https://itnext.io/flutter-1-17-no-more-flavors-no-more-ios-schemas-command-argument-that-solves-everything-8b145ed4285d

Show a Camera Activity which runs tflite on image frames in a flutter app

I have an android app having a CameraActivity which runs a tflite classifier periodically on image frames from the preview stream. The implementation of the Camera and tflite works great in the Android part and gives a good FPS.
I want to show this CameraActivity in my Flutter App as a Screen. The Flutter app has all the Frontend and UI part implemented already.
I've already tried using the official Flutter Camera plugin to implement the same by using camera.startImageStream but was unable to get matching FPS and the Camera Preview lags when calling the tflite model asynchronously using methodChannel.
I also came across AndroidView which embeds an Android view in the Widget hierarchy but the docs say it is an expensive operation and should be avoided when a Flutter equivalent is possible.
Is there a way to write a plugin for showing the CameraActivity (i.e. the UI) on the Flutter end similar to the way methodChannel is used for exchanging data between the Flutter and the Native codes. Or if there's another possible way of achieving this, please let me know.
Thanks in advance!
//Flutter side
Future<Null> showNativeView() async {
if (Platform.isAndroid) {//check if platform is android
var methodChannel = MethodChannel("methodchannelname");//create a method channel name
await methodChannel.invokeMethod('showNativeCameraView');//create method name
}
}
Container(
child: InkWell(
onTap: () {
showNativeView();//call to open native android activity
},
child: Center(
child: new Text("takeimage"))))
//Android side
//inside mainactivity on create
var channel: MethodChannel? = null
channel = MethodChannel(flutterView, "methodchannelname");
MethodChannel(flutterView, "methodchannelname")//same as flutterside
.setMethodCallHandler { call, result ->
if (call.method == "showNativeCameraView") {//same methodname as flutterside
val intent = Intent(this, ActivityCamera::class.java)//native camera activity code can be added in the android folder of the flutter application
startActivity(intent)
result.success(true)
} else {
result.notImplemented()
}
}
This link will help u further
https://medium.com/#Chetan/flutter-communicate-with-android-activity-or-ios-viewcontroller-through-method-channel-c11704429cd0

Direct call without pressing green button flutter in ios build

I m using call_number plugin for direct call in flutter but the problem is that this is only working for Android not for iOS how I can build this functionality in iOS build.
Create your calling function as;
_call() async {
if(numberToCall != null)
await CallNumber().callNumber(numberToCall);
}
example usage:
IconButton(onPressed: _call);

Categories

Resources