title: Making Flutter C/C++ interop example work
body: <p>I'm learning Flutter-C/C++ interop according to <a href="https://flutter.dev/docs/development/platform-integration/c-interop" rel="nofollow noreferrer">the official example</a>. However, the code I wrote according to the tutorial doesn't compile. There must be some misunderstanding but I have no idea where. Step 1 and 2 seem easy enough and I don't have errors, but after Step 3, the generated example code <code>/path/to/project/native_add/example/lib/main.dart</code> looks like this</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  import 'dart:async';
  
  import 'package:flutter/services.dart';
  import 'package:native_add/native_add.dart';
  
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatefulWidget {
    @override
    _MyAppState createState() =&gt; _MyAppState();
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
    String _platformVersion = 'Unknown';
  
    @override
    void initState() {
      super.initState();
      initPlatformState();
    }
  
    // Platform messages are asynchronous, so we initialize in an async method.
    Future&lt;void&gt; initPlatformState() async {
      String platformVersion;
      // Platform messages may fail, so we use a try/catch PlatformException.
      try {
        platformVersion = await NativeAdd.platformVersion;
      } on PlatformException {
        platformVersion = 'Failed to get platform version.';
      }
  
      // If the widget was removed from the tree while the asynchronous platform
      // message was in flight, we want to discard the reply rather than calling
      // setState to update our non-existent appearance.
      if (!mounted) return;
  
      setState(() {
        _platformVersion = platformVersion;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Center(
            child: Text('1 + 2 == ${nativeAdd(1, 2)}'),
          ),
        ),
      );
    }
  }
  </code></pre>
  
  <p><code>/path/to/project/native_add/example/lib/native_add.dart</code> looks like this </p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:ffi';  // For FFI
  import 'dart:io';   // For Platform.isX
  
  // Define physical lib: the lib binary file to integrate
  final DynamicLibrary nativeAddLib =
  Platform.isAndroid
      ? DynamicLibrary.open("libnative_add.so")
      : DynamicLibrary.open("native_add.framework/native_add");
  
  // Retrieve exportable API in the lib binary
  final int Function(int x, int y) nativeAdd =
  nativeAddLib
      .lookup&lt;NativeFunction&lt;Int32 Function(Int32, Int32)&gt;&gt;("native_add")
      .asFunction();
  
  </code></pre>
  
  <p>Running <code>main.dart</code> gives me errors:</p>
  
  <pre><code>Launching lib/main.dart on iPhone 11 Pro Max in debug mode...
  
  Compiler message:
  lib/main.dart:52:35: Error: Method not found: 'nativeAdd'.
            child: Text('1 + 2 == ${nativeAdd(1, 2)}'),
                                    ^^^^^^^^^
  lib/main.dart:52:35: Error: The method 'nativeAdd' isn't defined for the class '_MyAppState'.
   - '_MyAppState' is from 'package:native_add_example/main.dart' ('lib/main.dart').
  Try correcting the name to the name of an existing method, or defining a method named 'nativeAdd'.
            child: Text('1 + 2 == ${nativeAdd(1, 2)}'),
                                    ^^^^^^^^^
  Compiler failed on /Users/me/Desktop/_dev/playground/flutter/flutter_firstflutterapp_part2/native_add/example/lib/main.dart
  Error launching application on iPhone 11 Pro Max.
  
  </code></pre>
  
  <p>Where am I wrong?</p>