Я изучаю взаимодействие 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.
Где я ошибаюсь?