Method Channel Flutter (ios)

37 Views Asked by At

AppDelegate

@UIApplicationMain
class AppDelegate: FlutterAppDelegate,MessagingDelegate {

    lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 let controller = FlutterViewController(project: nil, nibName: nil, bundle: nil)
        print("flutter test print : 1122")
        let nativeChannel = FlutterMethodChannel(name: "test.flutter",
                                                      binaryMessenger: controller.binaryMessenger)
nativeChannel.setMethodCallHandler({
//            [weak self]
            (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
if call.method == "getMessage" {

                  result("Data from iOS")
                  
              } 
            else {
                  print("flutter test print : else")
                result(FlutterMethodNotImplemented)
              }
        })
flutterEngine.run();
GeneratedPluginRegistrant.register(with: self.flutterEngine); // self.flutterEngine
        return super.application(application, didFinishLaunchingWithOptions: launchOptions);
    
}

File.dart

class TestMethodchannel extends StatefulWidget {

  @override
  _TestMethodchannel createState() => _TestMethodchannel();
}


class _TestMethodchannel extends State<TestMethodchannel> {

  static const platform = MethodChannel('test.flutter');
  String _dataFromNative = 'No Data';

  Future<void> getNativeData() async {

    String? data;
    try {
      final String? result = await platform.invokeMethod<String>('getMessage');
      // final String? result = await platform.invokeMethod('getMessage',{"param1":1});
      data = result;
    } on PlatformException catch (e) {
      data = "Failed to get data: '${e.message}'.";
    }

    print('flutter data : '+data.toString());


    setState(() {
      _dataFromNative = data.toString();
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Native Data Fetcher'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: getNativeData,
              child: Text('Fetch Data from Native'),
            ),
            SizedBox(height: 20),
            Text('Data from Native: $_dataFromNative'),
          ],
        ),
      ),
    );
  }
}

Unable to send values ​​between iOS(Swift) and flutter. unknown cause. I've tried various code modifications but it still doesn't work. environment: sdk: '>=3.1.0 <4.0.0' flutter version 3.13.0 ..........................................................................................................................................................................................................

0

There are 0 best solutions below