【问题标题】:Flutter Application Freezing when download large file下载大文件时 Flutter 应用程序冻结
【发布时间】:2023-01-15 09:48:03
【问题描述】:

现在我制作了一个脚本来加密我的视频并将它们下载到应用程序存储,但是现在当我尝试下载小文件时我没有遇到任何问题,但是当我尝试下载大文件时我的应用程序卡在了99% 并花费大约 2 分钟将文件保存到应用程序存储。

`[![99% 的文件下载](https://i.stack.imgur.com/W3UUu.jpg)](https://i.stack.imgur.com/W3UUu.jpg)

我的代码

`

final String url = streamInfo.url.toString();
final dir = await getApplicationDocumentsDirectory();
String appDocPath = dir.path;
print("Downloading...");
var resp = await dio.get(url,
    options: Options(
      responseType: ResponseType.bytes,
      followRedirects: false,
    ),
    onReceiveProgress: (recivedBytes, totalBytes) {
      setState(() {
        progress = recivedBytes / totalBytes;
      });
    },
);
print(resp.data);

var encResult = _encryptData(resp.data);


_writeData(encResult, appDocPath + '/${widget.lessoneName.toString()}.aes');
print("File downloaded successfully");`
```

```
_encryptData(str){
  final encrypted = MyEncrypt.myEncrypt.encryptBytes(str,iv:MyEncrypt.myIv);
  return encrypted.bytes;
}
Future<String> _writeData(str,path) async{
  print("Writting data");
  File f = File(path);
  print(f);
  await f.writeAsBytes(str);
  return f.absolute.toString();
}
````


```
class MyEncrypt{
  static final myKey = esc.Key.fromUtf8('TechWithVPTechWithVPTechWithVP12');
  static final myIv = esc.IV.fromUtf8('VivekPanacha1122');
  static final myEncrypt = esc.Encrypter(esc.AES(myKey));
}
```

【问题讨论】:

  • 大文件有多大?
  • 您正在按顺序做 3 件事 .. 1) 下载数据,2) 加密数据 & 3) 将加密数据写入文件 .. 您的进度指示器仅显示第 1 步 .. 因此在第 2 步和第 3 步期间冻结。
  • @EbbeM.Pedersen 那么我该如何解决这个问题
  • @GrahamD 大约 200mb
  • Ebbe 已经给了你冻结的原因。您正在下载和处理大量数据。这需要时间,没有办法解决。您需要管理您的用户体验。看看在后台可以做什么,同时让用户满意。此外,在我来自的地方,移动数据很昂贵,用户可能不喜欢使用 200mb。你需要考虑这一点。

标签: flutter encryption flutter-test


【解决方案1】:

也许你已经找到了这个问题的解决方案 无论如何,我会为面临此类问题的其他人写出正确的解决方案

您必须使用 isolate 以字节形式读取数据,这样它才不会影响 UI

使用隔离,您将昂贵的操作发送到后台,这意味着您创建了一个新线程,以便在完成后捕获结果 顺便说一下,记住你的函数(在本例中以字节形式读取数据)应该是高级方法

这是示例代码

import 'dart:io';
import 'dart:isolate';

import 'package:dio/dio.dart' as dio;
import 'package:encrypt/encrypt.dart';
import 'package:path_provider/path_provider.dart';


class DownloadFileModel {
  final SendPort sendPort;
  final dio.Response<dynamic> response;
  final String savePath;

  DownloadVideoModel({
    required this.response,
    required this.sendPort,
    required this.savePath,
  });
}

class DownloadFile {
  dio.Dio request = dio.Dio();
  void downloadNewFile(String url) async {
    final dir = await getApplicationDocumentsDirectory();
    String appDocPath = dir.path;
    var resp = await request.get(
      url,
      options: dio.Options(
        responseType: dio.ResponseType.bytes,
        followRedirects: false,
      ),
      onReceiveProgress: (receivedBytes, totalBytes) {
        print(receivedBytes / totalBytes);
      },
    );
    ReceivePort port = ReceivePort();
    Isolate.spawn(
      whenDownloadCompleted,
      DownloadFileModel(
          response: resp, sendPort: port.sendPort, savePath: appDocPath),
    );
    port.listen((encryptedFilePath) {
      print(encryptedFilePath);
      port.close();
    });
  }
}

class MyEncrypt {
  static final myKey = Key.fromUtf8('TechWithVPTechWithVPTechWithVP12');
  static final myIv = IV.fromUtf8('VivekPanacha1122');
  static final myEncrypt = Encrypter(AES(myKey));
}

void whenDownloadCompleted(DownloadVideoModel model) async {
  SendPort sendPort = model.sendPort;
  var encryptResult =
      MyEncrypt.myEncrypt.encryptBytes(iv: MyEncrypt.myIv, model.response.data);
  File encryptedFile = File("${model.savePath}/myFile.aes");
  encryptedFile.writeAsBytes(encryptResult.bytes);
  sendPort.send(encryptedFile.absolute.path);
}

有关更多信息,请访问 flutter 官方文档站点

https://api.flutter.dev/flutter/dart-isolate/Isolate-class.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多