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เพิ่มพารามิเตอร์ให้กับฟังก์ชัน (แม้ว่าคุณจะไม่ต้องการก็ตาม) และใช้การคำนวณ มันจะทำงานได้อย่างสมบูรณ์

ก่อน

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

}