Flutter : 여러 메서드 실행

Oct 19 2020

큰 문제가 있습니다. 내 비디오 파일을 암호화하려는 경우 해당 방법이 완료 될 때까지 애플리케이션이 정지됩니다. 그러나 오류는 없습니다. 애플리케이션이 정지되지 않도록 코딩하려면 어떻게해야합니까? 감사.

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

답변

1 EmadFathy Oct 20 2020 at 09:22

함수의 함수 유형을에서 Future로 변경하고 함수 FutureOr에 매개 변수를 추가하고 (필요하지 않은 경우에도) compute를 사용합니다. 완벽하게 작동합니다.

전에

Future sifrele() async {

FutureOr sifrele(String para) async {

계산하다

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

또 다른 중요한 것은 함수의 정의가 sifrele최상위 수준이어야합니다. 즉, 클래스 내부가 아니라 클래스 외부에 두는 것을 의미합니다. doTheEncryption () 함수 (또는 이름이 무엇이든)는 클래스 내부에 문제가 없습니다.

savke Oct 19 2020 at 20:07

이 시도. 암호화를위한 코드를 분리하면 작동 할 수 있습니다. 그 사용 방법 계산.

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

}