Flutter: chạy nhiều phương thức
Tôi có một vấn đề lớn. Nếu tôi muốn mã hóa tệp video của mình, ứng dụng của tôi sẽ bị đóng băng cho đến khi phương pháp đó hoàn tất. Nhưng không có lỗi. Làm cách nào để mã ứng dụng của tôi không bị đóng băng. cảm ơn.
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;
}
}
Trả lời
Thay đổi kiểu hàm của hàm của bạn từ Futurethành FutureOr, thêm tham số vào hàm (ngay cả khi bạn không cần) và sử dụng tính toán. nó sẽ hoạt động hoàn hảo.
Trước
Future sifrele() async {
Sau
FutureOr sifrele(String para) async {
tính toán
doTheEncryption() {
compute(sifrele, 'Pass the value of the parameter here if you need it');
}
Một điều quan trọng khác là định nghĩa của hàm sifrelephải là cấp cao nhất, nghĩa là không nằm trong lớp, hãy đặt nó bên ngoài lớp. Hàm doTheEncryption () (hoặc tùy bạn đặt tên cho nó) có thể bên trong lớp không có vấn đề gì.
Thử cái này. Có thể sẽ hoạt động nếu bạn đặt mã để mã hóa riêng biệt. Đối với phương pháp sử dụng đó tính toán.
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;
}
}