I am making a flutter app and inside it the user can watch or download videos After downloading a video I want to encrypt it and then store it because I don t want the user to be able to share it. The encrypting process is being performed correctly however when it starts the app freezes for like 10 seconds before its finished and the app unfreezes I tried multiple encrypting methods but had the same result I read somewhere here that using "compute" methode and changing the function type to "FutureOr" can solve the problem. However, I cant seem to use it correctly. Any help would be appreciated.
Future<void> downloadVideo(String url) async {
var appDocDir = await getExternalStorageDirectory();
String appDocPath = appDocDir!.path;
String filePath = $appDocPath/Videos/${url.split( / ).last} ;
try {
var request = await HttpClient().getUrl(Uri.parse(url));
var response = await request.close();
List<int> bytes = [];
int downloaded = 0;
int total = response.contentLength!;
response.listen(
(List<int> newBytes) {
bytes.addAll(newBytes);
downloaded += newBytes.length;
downloadProgress.value = downloaded / total;
print("Download progress: ${downloadProgress.value}");
},
onDone: () async {
// Generate encryption key and IV
final key = encrypt.Key.fromSecureRandom(32);
final iv = encrypt.IV.fromSecureRandom(16);
// Encrypt the video bytes
final encrypter =
encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));
final encryptedBytes = encrypter.encryptBytes(bytes, iv: iv).bytes;
// Store the key and IV securely
final storage = FlutterSecureStorage();
await storage.write(key: key_$filePath , value: key.base64);
await storage.write(key: iv_$filePath , value: iv.base64);
// Save the encrypted video
File file = File(filePath);
await file.writeAsBytes(encryptedBytes);
print("Download completed: $filePath");
},
onError: (e) {
print("Error downloading video: $e");
},
cancelOnError: true,
);
} catch (e) {
print("Error downloading video: $e");
}
}