AppDelegate
class AppDelegate: FlutterAppDelegate,MessagingDelegate {
lazy var flutterEngine = FlutterEngine(title: "my flutter engine")
override func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let controller = FlutterViewController(mission: nil, nibName: nil, bundle: nil)
print("flutter take a look at print : 1122")
let nativeChannel = FlutterMethodChannel(title: "take a look at.flutter",
binaryMessenger: controller.binaryMessenger)
nativeChannel.setMethodCallHandler({
// [weak self]
(name: FlutterMethodCall, outcome: @escaping FlutterResult) -> Void in
if name.methodology == "getMessage" {
outcome("Knowledge from iOS")
}
else {
print("flutter take a look at print : else")
outcome(FlutterMethodNotImplemented)
}
})
flutterEngine.run();
GeneratedPluginRegistrant.register(with: self.flutterEngine); // self.flutterEngine
return tremendous.software(software, didFinishLaunchingWithOptions: launchOptions);
}```
File.dart
class TestMethodchannel extends StatefulWidget {
@override
_TestMethodchannel createState() => _TestMethodchannel();
}
class _TestMethodchannel extends State {
static const platform = MethodChannel(‘take a look at.flutter’);
String _dataFromNative=”No Knowledge”;
Future getNativeData() async {
String? knowledge;
attempt {
last String? outcome = await platform.invokeMethod<String>('getMessage');
// last String? outcome = await platform.invokeMethod('getMessage',{"param1":1});
knowledge = outcome;
} on PlatformException catch (e) {
knowledge = "Didn't get knowledge: '${e.message}'.";
}
print('flutter knowledge : '+knowledge.toString());
setState(() {
_dataFromNative = knowledge.toString();
});
}
@override
Widget construct(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Textual content(‘Native Knowledge Fetcher’),
),
physique: Middle(
youngster: Column(
mainAxisAlignment: MainAxisAlignment.middle,
youngsters: [
ElevatedButton(
onPressed: getNativeData,
child: Text(‘Fetch Data from Native’),
),
SizedBox(height: 20),
Text(‘Data from Native: $_dataFromNative’),
],
),
),
);
}
}“`
Unable to ship values between iOS(Swift) and flutter.
Should you use
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
an error will happen.