python - starflut 包 Flutter 的问题 - 应用程序无法启动
问题描述
我正在使用 Flutter 编写我的应用程序并希望集成 Python。我想为此使用包“starflut”(告诉我是否有更好的选择来使用 Python 后端),但是如果我按照https://smazee.com/blog/how-to-run中的步骤进行操作-the-python-code-in-the-flutter-app 应用程序立即启动和关闭。这是我的代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:starflut/starflut.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _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 {
StarCoreFactory starcore = await Starflut.getFactory();
StarServiceClass Service = await starcore.initSimple("test", "123", 0, 0, []);
await starcore
.regMsgCallBackP((int serviceGroupID, int uMsg, Object wParam, Object lParam) async {
print("$serviceGroupID $uMsg $wParam $lParam");
return null;
});
StarSrvGroupClass SrvGroup = await Service["_ServiceGroup"];
/*---script python--*/
bool isAndroid = await Starflut.isAndroid();
if (isAndroid == true) {
await Starflut.copyFileFromAssets(
"testcallback.py", "flutter_assets/starfiles", "flutter_assets/starfiles");
await Starflut.copyFileFromAssets(
"testpy.py", "flutter_assets/starfiles", "flutter_assets/starfiles");
await Starflut.copyFileFromAssets(
"python3.6.zip", "flutter_assets/starfiles", null); //desRelatePath must be null
await Starflut.copyFileFromAssets("zlib.cpython-36m.so", null, null);
await Starflut.copyFileFromAssets("unicodedata.cpython-36m.so", null, null);
await Starflut.loadLibrary("libpython3.6m.so");
}
String docPath = await Starflut.getDocumentPath();
print("docPath = $docPath");
String resPath = await Starflut.getResourcePath();
print("resPath = $resPath");
dynamic rr1 = await SrvGroup.initRaw("python36", Service);
print("initRaw = $rr1");
var Result = await SrvGroup.loadRawModule(
"python", "", resPath + "/flutter_assets/starfiles/" + "testpy.py", false);
print("loadRawModule = $Result");
dynamic python = await Service.importRawContext("python", "", "", false, "");
print("python = " + await python.getString());
StarObjectClass retobj = await python.call("tt", ["hello ", "world"]);
print(await retobj[0]);
print(await retobj[1]);
print(await python["g1"]);
StarObjectClass yy = await python.call("yy", ["hello ", "world", 123]);
print(await yy.call("__len__", []));
StarObjectClass multiply = await Service.importRawContext("python", "Multiply", "", true, "");
StarObjectClass multiply_inst = await multiply.newObject(["", "", 33, 44]);
print("DU GEILER HENGST");
print(await multiply_inst.getString());
print(await multiply_inst.call("multiply", [11, 22]));
await SrvGroup.clearService();
await starcore.moduleExit();
platformVersion = 'Python 3.6';
} on PlatformException catch (e) {
print("{$e.message}");
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 new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: new Text('Running on: $_platformVersion\n'),
),
),
);
}
}
任何人都知道,在这里做什么?
我只添加了两件事:
StarObjectClass multiply = await Service.importRawContext("python", "Multiply", "", true, "");
和
dynamic python = await Service.importRawContext("python", "", "", false, "");
(我在布尔左侧的位置添加了“”,因为预期有错误 5 个参数,但只找到了 4 个)。
解决方案
只需添加null
作为第一个参数。
dynamic python = await Service.importRawContext(null,"python", "", false, "");
推荐阅读
- intel - 为什么我们在 DH 安全会话建立中需要发起者或响应者的 enclave 身份?
- reactjs - 未考虑渲染中的 if 语句
- python-3.x - UnboundLocalError:分配前引用的局部变量“playercount”
- c# - 查找正则表达式模式匹配字符串有多个条件?
- django - 为什么我收到此错误?django.db.utils.OperationalError:致命:用户“postgres”的密码验证失败
- android - 使用 GridLayoutManager 在分页库中设置跨度大小
- go - 浮点型映射键
- c++ - OpenCV C++ 读取图像并修补 NaN 错误:patchNaNs 中的断言失败 (_a.depth() == CV_32F)
- java - 从文本文件中选择一个随机词
- reactjs - React Route 显示一个空白页面