Dart/Flutter HTTP response is incomplete when downloading large files

573

I wonder reason is because of timeout of http connection.
Would you try to give a long timeout?

static Future<String> fileFetch(String url) async
{
 try {
    final HttpClientRequest request = await 
    HttpClient().getUrl(Uri.parse(url));
    final HttpClientResponse response = await request.close().timeout(const 
      Duration(seconds: 100));;
    return await response.pipe(Accumulator());
  } on TimeoutException catch (e) {
    // handle timeout
  }
}

Share:
573
DroidOS
Author by

DroidOS

Updated on November 23, 2022

Comments

  • DroidOS
    DroidOS over 1 year

    I have a Dart/Flutter mobile app that configures itself by downloading files from a server. The files are ZLib compressed, headlessly, and this process has always worked very well - up until now. Here is the relevant code

    static Future<String> fileFetch(String url) async
    {
     final HttpClientRequest request = await HttpClient().getUrl(Uri.parse(url));
     final HttpClientResponse response = await request.close();
     return await response.pipe(Accumulator());
    }
    

    where the Acccumulator class handles the actual file download + ZLib decoding

    import 'dart:io';
    import 'dart:async';
    
    
    class Accumulator extends StreamConsumer<List<int>>
    {
     List<int> accu = List.empty(growable:true);
    
     String originalText()
     {
      ZLibCodec zlc = ZLibCodec(raw:true);
      print('Accu ${accu.length}');
      List<int> uncompd = zlc.decode(accu);
      String text = String.fromCharCodes(uncompd); 
      return text;
     }
    
     void whenData(List<int> data){accu.addAll(data);}
    
     void whenErr(e){XLog.e('Accu',e.toString());}
    
     @override
     Future<void> addStream(Stream<List<int>> s) async
     {
      s.listen(whenData);
      return; 
     }
    
     @override 
     Future<dynamic> close() async
     {
      return Future.value(originalText());
     }
    }
    

    For the most part the orignalText being returned is JSON. The offending file that has now started causing an incomplete file download is 132Kb in length and I find that only 42894 bytes of the original (compressed) file are being downloaded.

    Why is this happening - intrinsic limits on how much can be handled by the Accumulator?

  • DroidOS
    DroidOS over 2 years
    This has worked so I guess it might well have been an issue. In any case, thanks for this - launching an HTTP request without a defined timeout and a timeout handler smacks of bad practice.