Flutter: exécutez plusieurs méthodes
J'ai un gros problème. Si je veux crypter mon fichier vidéo, mon application se fige jusqu'à ce que cette méthode se termine. Mais il n'y a pas d'erreur. Comment puis-je coder mon application ne se fige pas. Merci.
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;
}
}
Réponses
Changez le type de fonction de votre fonction de Futureà FutureOr, ajoutez un paramètre à la fonction (même si vous n'en avez pas besoin) et utilisez compute. cela fonctionnera parfaitement.
Avant
Future sifrele() async {
Après
FutureOr sifrele(String para) async {
calculer
doTheEncryption() {
compute(sifrele, 'Pass the value of the parameter here if you need it');
}
Une autre chose importante, la définition de la fonction sifreledoit être un niveau supérieur, ce qui signifie pas à l'intérieur de la classe, la mettre en dehors de la classe. La fonction doTheEncryption () (ou quel que soit votre nom) peut-être à l'intérieur de la classe sans problème.
Essaye ça. Peut-être fonctionnera si vous mettez du code pour le cryptage en isolat. Pour cela, utilisez la méthode 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;
}
}