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

标签 c++ mobile flutter dart native

我正在根据 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 插件。

关于c++ - 使 Flutter C/C++ 互操作示例工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58270824/

相关文章:

c++ - 琐碎的分配器感知容器?

javascript - 任何只有一个 API 同时支持桌面和移动设备的 Javascript 框架?

flutter - 类型 '_InternalLinkedHashMap<String, dynamic>' 不是使用 http 包的类型 'String' 的子类型

ios - iBeacon 监控具有相同 UUID 和不同主要、次要的多个信标

html - SVG 图像未显示在 Webkit 浏览器中

dart - 使用 Inherited Widget 时如何处理 bloc?

arrays - 如何使用某些函数将 List<bool> 的所有数据设置为 false

c++ - 如何为QPushButton 重新添加按下/单击效果?

c++ - 这个2D数组如何访问分配的索引?

c++ - asio 计时器什么时候超出范围?