title: How do I use a Flutter MethodChannel to invoke a method in dart code from the native swift code?
body: <p>I have looked at many similar questions on this topic, but none of the solutions have worked for me.. I am developing an App in Flutter, but want to call a specific method in my <code>main.dart</code> file from <code>AppDelegate.swift</code> in the native iOS project.</p>
  <p>To remove all other variables I have extracted the issue into a fresh dart project. I am trying to call <code>setChannelText()</code> from <code>AppDelegate.swift</code> using <code>methodChannel.invokeMethod()</code>, but with no success.</p>
  <p>Does anybody know where I am going wrong? I know I'm not acting upon the &quot;name&quot; parameter in <code>methodChannel.invokeMethod()</code>, but that's because I only want the call to invoke the method at all...</p>
  <p>Here is my main.dart file:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  
  void main() {
    runApp(MyApp());
  }
  
  class MyApp extends StatefulWidget {
    @override
    _MyAppState createState() =&gt; _MyAppState();
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
    MethodChannel channel =
        new MethodChannel("com.example.channeltest/changetext");
    String centerText;
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          backgroundColor: Colors.purple,
          body: Center(
            child: Text(
              centerText,
              style: TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                fontSize: 30.0,
              ),
            ),
          ),
        ),
      );
    }
  
    @override
    void initState() {
      super.initState();
      this.channel.setMethodCallHandler((call) async =&gt; await setChannelText());
      this.centerText = "Hello World!";
    }
  
    Future setChannelText() async {
      Future.delayed(Duration(milliseconds: 200));
      setState(() =&gt; this.centerText = "Another Text.");
    }
  }</code></pre>
  </div>
  </div>
  </p>
  <p>And here is my AppDelegate.swift file:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import UIKit
  import Flutter
  
  @UIApplicationMain
  @objc class AppDelegate: FlutterAppDelegate {
      var methodChannel: FlutterMethodChannel!
    override func application(
      _ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions:                 
  
  [UIApplication.LaunchOptionsKey: Any]?
    ) -&gt; Bool {
      
      let rootViewController : FlutterViewController = window?.rootViewController as! FlutterViewController
      methodChannel = FlutterMethodChannel(name: "com.example.channeltest/changetext", binaryMessenger: rootViewController as! FlutterBinaryMessenger)
      
      //This call would obviously be somewhere else in a real world example, but I'm just
      //testing if I can invoke the method in my dart code at all..
      methodChannel.invokeMethod("some_method_name", arguments: nil)
      
      GeneratedPluginRegistrant.register(with: self)
      return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
  }</code></pre>
  </div>
  </div>
  </p>
  <p>In the end, I am trying to get the text to change right after launch, but it doesn't.</p>
  <p><a href="https://i.stack.imgur.com/9lVT3.png" rel="nofollow noreferrer">Screenshot of app running on iOS simulator</a></p>
  <p>Thanks in advance for any help!!</p>