Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 298 Vote(s) - 3.63 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass a message from Flutter to Native?

#1
How would you pass info from Flutter back to Android/Native code if needed to interact with a specific API / hardware component?

Are there any Event Channels that can send info the other way or something similar to a callback?

1. The [platform_channel][1] documentation points out "method calls can also be sent in the reverse direction, with the platform acting as client to methods implemented in Dart. A concrete example of this is the [quick_actions plugin][2]." I don't see how the native side is receiving a message from Flutter in this instance.
2. It looks like a [BasicMessageChannel][3]’s send() method can be used to send "the specified message to the platform plugins on this channel". Can anyone provide a simple implementation example of this?


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#2
Yes, flutter does has an [`EventChannel`](

[To see links please register here]

) class which is what you are looking for exactly.

[Here](

[To see links please register here]

) is an example of that demonstrates how `MethodChannel` and `EventChannel` can be implemented. And
[this](

[To see links please register here]

) medium article shows how an `EventChannel` can be implemented in flutter.

Hope that helped!
Reply

#3
**Objective C**

```
call.arguments[@"parameter"]
```

**Android**

```
call.argument("parameter");
```
Reply

#4
for swift

guard let args = call.arguments as? [String : Any] else {return}
let phoneNumber = args["contactNumber"] as! String
let originalMessage = args["message"] as! String
Reply

#5
If anyone wants to share the data from native to flutter with invoke method
follow this:

main.dart

Future<dynamic> handlePlatformChannelMethods() async {
platform.setMethodCallHandler((methodCall) async {
if (methodCall.method == "nativeToFlutter") {
String text = methodCall.arguments;
List<String> result = text.split(' ');
String user = result[0];
String message = result[1];
}
}
}

MainActivity.java

nativeToFlutter(text1:String?,text2:String?){
MethodChannel(flutterEngine!!.dartExecutor.binaryMessenger,
CHANNEL.invokeMethod("nativeToFlutter",text1+" "+text2);
}
Reply

#6
This is a simple implementation showcasing:

1. Passing a string Value from flutter to Android code
2. Getting back response from Android code to flutter

Code is based on example from:

[To see links please register here]


1. Passing string value "text":

```dart
String text = "whatever";

Future<Null> _getBatteryLevel(text) async {
String batteryLevel;
try {
final String result = await platform.invokeMethod('getBatteryLevel',{"text":text});
batteryLevel = 'Battery level at $result % .';
} on PlatformException catch (e) {
batteryLevel = "Failed to get battery level: '${e.message}'.";
}

setState(() {
_batteryLevel = batteryLevel;
});

2. Getting back response "batterylevel" after RandomFunction();

```dart
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("getBatteryLevel")) {

text = call.argument("text");
String batteryLevel = RandomFunction(text);

if (batteryLevel != null) {
result.success(batteryLevel);
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
} else {
result.notImplemented();
}
}
```
Reply

#7
Passing from Flutter to native:

await platform.invokeMethod('runModel',
{'path': imagePath!.path} // 'path' is the key here to be passed to Native side
);

For Android (Kotlin):

val hashMap = call.arguments as HashMap<*,*> //Get the arguments as a HashMap

val path = hashMap["path"] //Get the argument based on the key passed from Flutter

For iOS (Swift):

guard let args = call.arguments as? [String : Any] else {return}
let text = args["path"] as! String
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through