Flutter download flie in stream for progress control for iOS devices

Viewed 47

This code work well at Android and at Android Studio framework, in same time if code deployed to iOS devices i receive error

I reciving OS (Exhausted heap space, trying to allocate 1073741840 bytes. [VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: Out of )

  await response.stream.listen(
      (List<int> newBytes) {
        bytes.addAll(newBytes);
        final downloadedLength = bytes.length;
        _progress = (downloadedLength / contentLength!) / 2 * 100;
        if(bytes.length>96607667)
          {
            print (bytes.length);
          }
        if(newBytes.length>96607667)
        {
          print (newBytes.length);
        }
        fileInfo.progress = _progress;
      },
  cancelOnError: true
).asFuture();

size of file is 96607667 i can't understand - what i do wrong?

1 Answers

I solved the problem in the following way

 late Uint8List bytes;
  int received = 0;

  var completer = Completer<Uint8List>();

  var sink = ByteConversionSink.withCallback(
          (bytes) => completer.complete(Uint8List.fromList(bytes)));

  response.stream.listen(
          (List<int> newBytes) {
            sink.add(newBytes);

            received += newBytes.length;
            fileInfo.progress = (received / contentLength) / 2 * 100;
          },
      onError: completer.completeError,
      onDone: sink.close,
      cancelOnError: true);
Related