我正在用flatter构建一个应用程序,我需要使用一些Python代码。为此,我使用Starflut插件(https://pub.dev/packages/starflut)。我成功地运行了Python代码,这里没有问题,但我想导入额外的包
为此,我创建了一个应用所在的虚拟环境,然后通过pip安装程序numpy、cv2和Pillow进行下载,但仍然没有问题,下载顺利
但是,例如,当我尝试导入numpy时,我的IDE(Visual Studio代码)告诉我有一个错误:
Unable to import 'numpy'
下面是我的main.dart代码(唯一的dart文件,它是一个测试应用程序):
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:starflut/starflut.dart';
void main() => runApp(Main());
class Main extends StatefulWidget {
@override
_MainState createState() => _MainState();
}
class _MainState extends State<Main> {
var _outputString = '';
StarSrvGroupClass srvGroup;
dynamic python;
_MainState() {
_initStarCore();
}
void showOutput(String info) async {
if (info == null || info.length == 0) return;
_outputString = info;
setState(() {});
}
void _initStarCore() async {
StarCoreFactory starcore = await Starflut.getFactory();
StarServiceClass service =
await starcore.initSimple("test", "123", 0, 0, []);
String resPath = await Starflut.getResourcePath();
await starcore.regMsgCallBackP(
(int serviceGroupID, int uMsg, Object wParam, Object lParam) async {
if (uMsg == Starflut.MSG_DISPMSG || uMsg == Starflut.MSG_DISPLUAMSG) {
showOutput(wParam);
}
print("$serviceGroupID $uMsg $wParam $lParam");
return null;
});
srvGroup = await service["_ServiceGroup"];
bool isAndroid = await Starflut.isAndroid();
if (isAndroid == true) {
String libraryDir = await Starflut.getNativeLibraryDir();
String docPath = await Starflut.getDocumentPath();
if (libraryDir.indexOf("arm64") > 0) {
Starflut.unzipFromAssets("lib-dynload-arm64.zip", docPath, true);
} else if (libraryDir.indexOf("x86_64") > 0) {
Starflut.unzipFromAssets("lib-dynload-x86_64.zip", docPath, true);
} else if (libraryDir.indexOf("arm") > 0) {
Starflut.unzipFromAssets("lib-dynload-armeabi.zip", docPath, true);
} else {
//x86
Starflut.unzipFromAssets("lib-dynload-x86.zip", docPath, true);
}
await Starflut.copyFileFromAssets("python3.6.zip",
"flutter_assets/starfiles", null); //desRelatePath must be null
await Starflut.copyFileFromAssets(
"program.py", "flutter_assets/starfiles", "flutter_assets/starfiles");
}
if (await srvGroup.initRaw("python36", service) == true) {
_outputString = "init starcore and python 3.6 successfully";
} else {
_outputString = "init starcore and python 3.6 failed";
}
await srvGroup.loadRawModule("python", "",
resPath + "/flutter_assets/starfiles/" + "program.py", false);
python = await service.importRawContext("python", "", false, "");
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
//_counter++;
});
}
void runScriptCode() async {
var result = await python.call("multiply", [5, 2]);
_outputString = result.toString();
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: FlatButton(
color: Colors.red,
child: Text(_outputString),
onPressed: () {
runScriptCode();
},
),
),
),
);
}
}
和我的Python文件:
import numpy as np # (to test)
def multiply(a, b):
return a*b
下面是我的树结构,如果它能帮助: The tree structure of my Flutter project
希望你能帮助我
提前谢谢
如果您想在android应用程序中集成python软件包,可以使用chaquopy插件进行颤振
如果可以的话,将python后端连接到Flatter,因为这样你的应用程序就会平滑
相关问题 更多 >
编程相关推荐