使 Flutter C/C++ 互操作示例工作

Making Flutter C/C++ interop example work

我正在根据 the official example 学习 Flutter-C/C++ 互操作。但是,我根据教程编写的代码无法编译。一定有一些误会,但我不知道在哪里。第 1 步和第 2 步看起来很简单而且我没有错误,但是在第 3 步之后,生成的示例代码 /path/to/project/native_add/example/lib/main.dart 看起来像这样

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:native_add/native_add.dart';


void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> 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)}'),
        ),
      ),
    );
  }
}

/path/to/project/native_add/example/lib/native_add.dart 看起来像这样

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<NativeFunction<Int32 Function(Int32, Int32)>>("native_add")
    .asFunction();

运行 main.dart 给我错误:

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.

我哪里错了?

您必须导入 /path/to/project/native_add/example/lib/native_add.dart,因为您导入了 native_add 插件。