Flutter: ejecuta varios métodos

Oct 19 2020

Tengo un gran problema. Si quiero cifrar mi archivo de video, mi aplicación se congelará hasta que termine ese método. Pero no hay error. ¿Cómo puedo codificar que mi aplicación no se congele? Gracias.

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

Respuestas

1 EmadFathy Oct 20 2020 at 09:22

Cambie el tipo de función de su función de Futurea FutureOr, agregue un parámetro a la función (incluso si no lo necesita) y use compute. funcionará perfectamente.

antes de

Future sifrele() async {

Después

FutureOr sifrele(String para) async {

calcular

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

Otra cosa importante, la definición de la función sifreledebe ser un nivel superior, es decir, no dentro de la clase, colóquela fuera de la clase. La función doTheEncryption () (o como se llame) tal vez dentro de la clase no sea un problema.

savke Oct 19 2020 at 20:07

Prueba esto. Tal vez funcione si pones el código para el cifrado aislado. Para ese uso use el método 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;
}

}