Last active
April 21, 2023 03:19
-
-
Save ctrleffive/3a46395a8b20215254d4ff5432bdb247 to your computer and use it in GitHub Desktop.
Flutter platform channel sample code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
import io.flutter.app.FlutterActivity; | |
import io.flutter.plugin.common.MethodCall; | |
import io.flutter.plugin.common.MethodChannel; | |
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | |
import io.flutter.plugin.common.MethodChannel.Result; | |
// ... | |
public class MainActivity extends FlutterActivity { | |
private static final String PLATFORM_CHANNEL = "elaenor.inometrics.com/platform_channel"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
new MethodChannel(getFlutterView(), PLATFORM_CHANNEL).setMethodCallHandler( | |
new MethodCallHandler() { | |
@Override | |
public void onMethodCall(MethodCall call, Result result) { | |
// if (call.method.equals("demoFunction")) { // INFO: method check | |
// String argument = call.argument("data"); // INFO: get arguments | |
// demoFunction(result, argument); // INFO: method call, every method call should pass result parameter | |
// } else | |
if (call.method.equals("getNetworks")) { | |
getNetworks(result); | |
} | |
else if (call.method.equals("connectNetwork")) { | |
String ssid = call.argument("ssid"); | |
String password = call.argument("password"); | |
connectNetwork(result, ssid, password); | |
} | |
else { | |
result.notImplemented(); // INFO: not implemented | |
} | |
} | |
} | |
); | |
GeneratedPluginRegistrant.registerWith(this); | |
} | |
public void demoFunction(Result result, String data) { | |
// INFO: function implementation | |
if (true) { // INFO: check for some condition | |
result.success("android call success with data " + data); // INFO: success response should return through this method | |
} else { | |
result.error("ERROR", "Error message description!", null); // INFO: error response should return through this method | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/services.dart'; | |
const PLATFORM_CHANNEL = const MethodChannel('<APP_BUNDLE_NAME>/platform_channel'); | |
// you can use whatever you like. but make sure you use the same channel string in native code also | |
// platform channel method calling | |
Future<Null> demoFunction(BuildContext context) async { | |
try { | |
final String result = await PLATFORM_CHANNEL.invokeMethod( | |
'demoFunction', // call the native function | |
<String, dynamic> { // data to be passed to the function | |
'data': 'sample data', | |
} | |
); | |
// result hold the response from plaform calls | |
} on PlatformException catch (error) { // handle error | |
print('Error: $error'); // here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
@UIApplicationMain | |
@objc class AppDelegate: FlutterAppDelegate { | |
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
GeneratedPluginRegistrant.register(with: self) | |
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController; | |
let PLATFORM_CHANNEL = FlutterMethodChannel.init(name: "elaenor.inometrics.com/platform_channel", binaryMessenger: controller); | |
PLATFORM_CHANNEL.setMethodCallHandler({ | |
(call: FlutterMethodCall, result: FlutterResult) -> Void in | |
// if ("demoFunction" == call.method) { // INFO: method check | |
// let arguments = call.arguments as! NSDictionary // INFO: get arguments | |
// demoFunction(result: result, data: arguments["data"] as! String) // INFO: method call, every method call should pass result parameter | |
// } else | |
if ("getNetworks" == call.method) { | |
getNetworks(result: result) // INFO: method call | |
} | |
else if ("connectNetwork" == call.method) { | |
// Connect to a network with provided SSID & Password | |
// This will not work, because we have no permission. So always return an error message. | |
result(FlutterError.init( | |
code: "NO_PERMISSION", | |
message: "Auto connect not possible in iOS devices! Please connect to network manually.", | |
details: nil | |
)) | |
} | |
else { | |
result(FlutterMethodNotImplemented) | |
} | |
}) | |
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | |
} | |
} | |
func demoFunction(result: FlutterResult, data: String) { | |
// INFO: function implementation | |
if (true) { // INFO: check for some condition | |
result("ios call success with data \(data)") // INFO: success response should return through this method | |
} else { | |
result(FlutterError.init( | |
code: "ERROR", | |
message: "Error message description!", | |
details: nil | |
)) // INFO: error response should return through this method | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey @deremakif, thanks for letting me know about this. this gist is an old one. ill update it.