English 中文(简体)
加密程序冻结了冲碎机,直至其完成。
原标题:Encrypting process makes flutter app frozen until its done

I am making a flutter app and inside it the user can watch or download videos After downloading a video I want to encrypt it and then store it because I don t want the user to be able to share it. The encrypting process is being performed correctly however when it starts the app freezes for like 10 seconds before its finished and the app unfreezes I tried multiple encrypting methods but had the same result I read somewhere here that using "compute" methode and changing the function type to "FutureOr" can solve the problem. However, I cant seem to use it correctly. Any help would be appreciated.

  Future<void> downloadVideo(String url) async {
    var appDocDir = await getExternalStorageDirectory();
    String appDocPath = appDocDir!.path;
    String filePath =  $appDocPath/Videos/${url.split( / ).last} ;

    try {
      var request = await HttpClient().getUrl(Uri.parse(url));
      var response = await request.close();
      List<int> bytes = [];
      int downloaded = 0;
      int total = response.contentLength!;
      response.listen(
        (List<int> newBytes) {
          bytes.addAll(newBytes);
          downloaded += newBytes.length;
          downloadProgress.value = downloaded / total;
          print("Download progress: ${downloadProgress.value}");
        },
        onDone: () async {
          // Generate encryption key and IV
          final key = encrypt.Key.fromSecureRandom(32);
          final iv = encrypt.IV.fromSecureRandom(16);

          // Encrypt the video bytes
          final encrypter =
              encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));
          final encryptedBytes = encrypter.encryptBytes(bytes, iv: iv).bytes;

          // Store the key and IV securely
          final storage = FlutterSecureStorage();
          await storage.write(key:  key_$filePath , value: key.base64);
          await storage.write(key:  iv_$filePath , value: iv.base64);

          // Save the encrypted video
          File file = File(filePath);
          await file.writeAsBytes(encryptedBytes);
          print("Download completed: $filePath");
        },
        onError: (e) {
          print("Error downloading video: $e");
        },
        cancelOnError: true,
      );
    } catch (e) {
      print("Error downloading video: $e");
    }
  }
问题回答

缺席 Dart公司在一只read子里完成所有任务。 因此,当启动大型计算过程时,你可能在国库中滞后。

在此情况下,你可以利用电梯将重负荷转移,由完全不同的线子处理。 你可以简单地利用<代码>Isolate.run方法这样做。 不用直接使用<代码>downloadVideo方法,将其称作主线以下代码:

  final url = "..."; // Your download URL

  Isolate.run(() async {
    await downloadVideo(url);
  });

https://youtu.be/PPwJ75vqP_s”rel=“nofollow noreferer”Isolates onube Flutter and on Dart documentation

希望:





相关问题
Extend Contacts application on Android to provide encryption

I want to encrypt individual contacts stored by the Contacts application on Android based on user s preference. So, I am thinking I ll have to hook/extend the Contacts application before the it stores ...

Make md5 strong

Im making a website that will intergrate with game that only support md5 hashing metod (atm). Which ofc is not especially safe anymore. But how could i make it stronger? Should I just generate long ...

How to Pack/Encrypt/Unpack/Decrypt a bunch of files in Java?

I m essentially trying to do the following on a Java/JSP-driven web site: User supplies a password Password is used to build a strongly-encrypted archive file (zip, or anything else) containing a ...

Thread & Queue vs Serial performance

I though it ll be interesting to look at threads and queues, so I ve written 2 scripts, one will break a file up and encrypt each chunk in a thread, the other will do it serially. I m still very new ...

Convert PHP encryption code to C#

I m trying to convert this piece of code from PHP to C#. It s part of a Captive Portal. Could somebody explain what it does? $hexchal = pack ("H32", $challenge); if ($uamsecret) { $newchal = ...

Encryption: how to have 1 iv despite multiple fields

I ve been stuck trying to arrive at a best solution for this for a while. I know that an initialization vector has to be unique for each item being encrypted. So if I m encrypting an address and I ...

热门标签