Passing data between AFE_Android and AFE_Flutter

Receiving the data in AFE_Flutter and showing it in UI.

Future<void> _receiveFromHost(MethodCall call) async {

int f = 0;

int s = 0;



try {

print(call.method);



if (call.method == "fromHostToClient") {

final String data = call.arguments;

print(call.arguments);

final jData = jsonDecode(data);



f = jData['first'];

s = jData['second'];

}

} on PlatformException catch (e) {

//platform may not able to send proper data.

}



setState(() {

_first = f;

_second = s;

});

}

Also, remove hardcoded numbers in AFE_Flutter and set _first and _second as a text in both the text view.

Performing selected operation and sending results back to the host app.

void _sendResultsToAndroidiOS() {

if (dropdownValue == 'Add') {

_result = _addNumbers(_first, _second);

} else {

_result = _multiplyNumbers(_first, _second);

}



Map<String, dynamic> resultMap = Map();

resultMap['operation'] = dropdownValue;

resultMap['result'] = _result;



setState(() {

resultStr = resultMap.toString();

});



platform.invokeMethod("FromClientToHost", resultMap));

}

Now, we need to process the data received from the client app in FlutterViewActivity and send it back to InputNumbersActivity to show it in the UI.

MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->

// manage method calls here

if (call.method == "FromClientToHost") {

val resultStr = call.arguments.toString()

val resultJson = JSONObject(resultStr)

val res = resultJson.getInt("result")

val operation = resultJson.getString("operation")



val intent = Intent()

intent.putExtra("result", res)

intent.putExtra("operation", operation)

setResult(Activity.RESULT_OK, intent)

finish()

} else {

result.notImplemented()

setResult(Activity.RESULT_CANCELED)

finish()

}

}

and in InputNumbersActivity.kt’s onActivityResult method,

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

super.onActivityResult(requestCode, resultCode, data)



if (requestCode == 100) {

if (resultCode == Activity.RESULT_OK) {

val result = data?.extras?.getInt("result")

val operation = data?.extras?.getString("operation")



tvResult.text = "${when (operation) {

"Add" -> "Addition"

"Multiply" -> "Multiplication"

else -> "NA"

}} of the entered numbers is $result"

} else {

tvResult.text = "Could not perform the operation"

}

}

}

We are done!! AFE_Android should show the result of the operation being performed on input numbers. You can find the full source code on these Github Repositories, AFE_Android and AFE_Flutter.

Passing data between AFE_iOS and AFE_Flutter

Creating a data channel

//Set up data channel to send/receive data from AFE_Flutter let afeDataChannel = FlutterMethodChannel(name: "in.androidgeek.afe/data",binaryMessenger: flutterViewController)

Sending input numbers from AFE_iOS to AFE_Flutter

//Sending data to AFE_Flutter let jsonObject: NSMutableDictionary = NSMutableDictionary() jsonObject.setValue(first, forKey: "first") jsonObject.setValue(second, forKey: "second")

var convertedString: String? = nil do{ let data1 = try JSONSerialization.data(withJSONObject: jsonObject, options: JSONSerialization.WritingOptions.prettyPrinted) convertedString = String(data: data1, encoding: String.Encoding.utf8) } catch let myJSONError { print(myJSONError) } afeDataChannel.invokeMethod("fromHostToClient", arguments: convertedString)

Receiving results from AFE_Flutter after the operation is being performed.

//setting up method call handler to receive data from AFE_Flutter afeDataChannel.setMethodCallHandler({ [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in //receiving data from AFE_Flutter and showing results in UI guard call.method == "FromClientToHost" else { result(FlutterMethodNotImplemented) return } flutterViewController.dismiss(animated: true, completion: nil) var dictonary:NSDictionary? = call.arguments as? NSDictionary if(call.arguments != nil){ self?.lResult.text = "\((dictonary!["operation"] as! String)=="Add" ? "Addition" : "Multiplication"): \(dictonary!["result"]!)" }else{ self?.lResult.text = "Could not perform the operation" } })

We have successfully integrated AFE_Flutter into AFE_iOS app.

Conclusion

Even though Flutter’s Add Flutter to existing apps is still in preview, you can start using it in the production app. Rather than developing your existing app completely in Flutter, you can start developing new features in Flutter and reuse your existing Android/iOS codebase.

The complete project source can be found on below Github Repositories:

AFE_Android: https://github.com/nirav-tukadiya/AFEAndroid

AFE_iOS: https://github.com/nirav-tukadiya/AFEiOS

AFE_Flutter: https://github.com/nirav-tukadiya/AFE_flutter