Flutter: uruchom wiele metod
Mam duży problem. Jeśli chcę zaszyfrować plik wideo, moja aplikacja zawiesza się do czasu zakończenia tej metody. Ale nie ma błędu. Jak mogę kodować, moja aplikacja nie zawiesza się. dzięki.
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;
}
}
Odpowiedzi
Zmień typ funkcji swojej funkcji z Futurena FutureOr, dodaj parametr do funkcji (nawet jeśli go nie potrzebujesz) i użyj obliczeń. będzie działać idealnie.
Przed
Future sifrele() async {
Po
FutureOr sifrele(String para) async {
obliczać
doTheEncryption() {
compute(sifrele, 'Pass the value of the parameter here if you need it');
}
Kolejną ważną rzeczą jest definicja funkcji sifrelenajwyższego poziomu, czyli nie wewnątrz klasy, umieszczenie jej poza klasą. Funkcja doTheEncryption () (lub jakkolwiek ją nazwiesz) może nie być problemem wewnątrz klasy.
Spróbuj tego. Może zadziała, jeśli umieścisz kod do szyfrowania w izolacji. W tym celu użyj metody obliczeniowej.
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;
}
}