Flutter: run multiple methods

Issue

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

Solution

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.

Before

Future sifrele() async {

After

FutureOr sifrele(String para) async {

compute

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

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.

Answered By – EmadFathy

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *