1
  void sendData() async {
    final session = StateInheritedWidget.of(context).state.session;
    String Datas;
    try {
      final String result = await sendData.invokeMethod(
          'sendData',{"bVolt":1.5,"hstring":2.5,
            "qString":3.5,"ExternalDeg":5.5,
        "extDeg":6.5,"sessionNo":int.tryParse(session) ?? 0});
      Datas = 'sendData response $result % .';
    } on PlatformException catch (e) {
      Datas = "Failed to get session response: '${e.message}'.";
    }
  }

Here I am passing a string data, I want to pass object data from database and send to android java and retrieve that data from java , How its possible through method channel?

4
  • 2
    you are pass it as map right now? are you? Commented Oct 12, 2022 at 10:53
  • @eamirho3ein yes can you share a sample code Commented Oct 12, 2022 at 11:18
  • sample of what? Commented Oct 12, 2022 at 11:19
  • @eamirho3ein how to send list of data through method channel Commented Oct 12, 2022 at 11:19

1 Answer 1

1

You can Send it as List of Map like this:

List<Map> data = [
        {
           "bVolt":1.5,
           "hstring":2.5,
           "qString":3.5,
           "ExternalDeg":5.5,
           "extDeg":6.5,
           "sessionNo":int.tryParse(session) ?? 0
        }
];

final String result = await sendData.invokeMethod('sendData',data);

and in android part (kotlin) you can try this:

val data = (call.arguments) as List;
for (item:Map<String, Any> in data) {
  Log.d("", "item = $item");  
}

and in your java part you can make a for loop and access your data:

new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
        .setMethodCallHandler(
          (call, result) -> {
               if (call.method.equals("sendData")) {
                 var data = (List<Map<String, Object>>)call.arguments;
                 for (Map<String, Object> item : data){
                     Log.d("", "item = $item");  
                 }
               } else {
                 result.notImplemented();
               }
          }
        );
Sign up to request clarification or add additional context in comments.

12 Comments

how to retrieve this data from java code?
when i added this on my java code getting issues and errors. , I think its kotlin code not working in java.
i don't know much about koltin and android , can you convert this code to android java.
what error do you get?
Unknown class: 'call.arguments' , Cannot resolve symbol 'Any' , Cannot resolve symbol 'as' , Cannot resolve symbol 'val' , these are the issues .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.