Johnykutty
Johnykutty

Reputation: 12859

Get data from flutter in Native code without FlutterViewController

I have both iOS and Android native projects and now I am developing a flutter module that can be accessed from both of the platforms. I followed documentation here for adding flutter to existing application.

I have used method channel for communication between native and flutter components. Now I want to get some data from flutter component in native part. What I am doing is creating method channel with flutter engine as the binary messenger.

    let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine!
    let channel = FlutterMethodChannel(name: "my.channel.name"
                                       binaryMessenger: flutterEngine)
    channel.invokeMethod("get_value", arguments: nil) { (value) in
        guard let string = value as? String else { return }
        self.valueLabel.text = "value received : " + string
    }

And in flutter

class CommunicationChannel {
  static const channel = MethodChannel("my.channel.name");

  void startListening() {
    print(" startListening........... ");

    channel.setMethodCallHandler((MethodCall call) async {
      print("channel method " + call.method);
      switch (call.method) {

        case 'get_value':

          return myValue
        default:
          throw MissingPluginException();
      }
    });
  }
}

main() function

void main() {
  print(" main() ------------------------. ");

  CommunicationChannel channel = CommunicationChannel();
  channel.startListening();

  runApp(new MyApp());
}

My concern is, even though I am not present any view here, a widget is created in main function which is unused.
Is there any way to read values from flutter in native code without creating any view/widget?

Upvotes: 3

Views: 1352

Answers (0)

Related Questions