Flutter: esegui più metodi

Oct 19 2020

Ho un grosso problema. Se voglio crittografare il mio file video, la mia applicazione si blocca fino al termine del metodo. Ma non ci sono errori. Come posso codificare la mia applicazione non si blocca. Grazie.

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

Risposte

1 EmadFathy Oct 20 2020 at 09:22

Cambia il tipo di funzione della tua funzione da Futurea FutureOr, aggiungi un parametro alla funzione (anche se non ne hai bisogno) e usa compute. funzionerà perfettamente.

Prima

Future sifrele() async {

Dopo

FutureOr sifrele(String para) async {

calcolare

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

Un'altra cosa importante la definizione della funzione sifreledeve essere un livello superiore, il che significa non all'interno della classe, ma metterla fuori dalla classe. La funzione doTheEncryption () (o comunque tu la chiami) forse all'interno della classe nessun problema.

savke Oct 19 2020 at 20:07

Prova questo. Forse funzionerà se metti il ​​codice per la crittografia in isolamento. Per questo uso il metodo 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;
}

}