Flutter:运行多种方法 [英] Flutter: run multiple methods
问题描述
我有一个大问题.如果我想加密我的视频文件,我的应用程序将冻结直到该方法完成.但是没有错误.我该如何编码我的应用程序不会冻结.谢谢.
I have a big problem. If I want to encrypt my video file, my application is freezing until that method finishing. But there is no error. How can I code my application does not freeze. thanks.
Future sifrele() async {
String realPath =
"/storage/emulated/0/Android/data/com.android.xxxx/files";
var crypt = AesCrypt('sefa');
try {
crypt.setOverwriteMode(AesCryptOwMode.on);
String encFilepaths = await crypt.encryptFile(
realPath + '/WhatCarCanYouGetForAGrand.mp4',
realPath + '/video.mp4.aes');
print('The encryption has been completed successfully.');
//print('Encrypted file: $encFilepath');
} on AesCryptException catch (e) {
if (e.type == AesCryptExceptionType.destFileExists) {
print('The encryption has been completed unsuccessfully.');
}
return;
}
}
推荐答案
将函数的函数类型从Future
更改为FutureOr
,向函数添加参数(即使不需要)
并使用计算.它会完美地工作.
Change the function type of your function from Future
to FutureOr
, add a parameter to the function (even if you don't need it)
and use compute. it will work perfectly.
之前
Future sifrele() async {
之后
FutureOr sifrele(String para) async {
计算
doTheEncryption() {
compute(sifrele, 'Pass the value of the parameter here if you need it');
}
函数sifrele
的另一重要定义必须是顶层,这意味着不在类内部,而应将其置于类外部.
函数 doTheEncryption()(或您所说的任何名称)也许在类内部都没有问题.
Another one important thing the definition of the function sifrele
must be a top level, meaning not inside the class, put it outside the class.
The function doTheEncryption() (or whaterver you name it) maybe inside the class no problem.
这篇关于Flutter:运行多种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!