Flutter:运行 多种方法

Flutter: run multiple methods

我有一个大问题。如果我想加密我的视频文件,我的应用程序会冻结,直到该方法完成。但是没有错误。我怎样才能编码我的应用程序不冻结。谢谢。

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;
    }
  }

试试这个。 如果您将加密代码单独放置,也许会起作用。 对于那个使用方法 compute.

void _startEncrypting()
{
compute(sifrele, /** Here put your parameters if you need them **/);
}

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,为函数添加一个参数(即使您不需要它) 并使用计算。它将完美运行。

之前

Future sifrele() async {

之后

FutureOr sifrele(String para) async {

计算

  doTheEncryption() {
    compute(sifrele, 'Pass the value of the parameter here if you need it');
  }

另外一个重要的事情是函数的定义sifrele必须是顶层的,意思是不在class里面,放在class外面。 函数 doTheEncryption()(或者随便你怎么命名)可能在 class 里面没问题。