Skip to content

ByteStream should work with bytes (Uint8List), not List<int> #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mnordine opened this issue Nov 16, 2019 · 1 comment
Closed

ByteStream should work with bytes (Uint8List), not List<int> #343

mnordine opened this issue Nov 16, 2019 · 1 comment

Comments

@mnordine
Copy link
Contributor

mnordine commented Nov 16, 2019

I was surprised when streaming in a response, the chunks were List<int>, not Uint8List

var bytes = 0;
await for (final chunk in response.stream) {
  bytes += chunk.lengthInBytes; // nope, it's a List<int>
}
@jamesderlin
Copy link

jamesderlin commented Feb 12, 2022

Is this still a problem? I find it hard to believe that it is. If so, can anyone provide minimal, complete code that can reproduce the problem? A runtime error doesn't make sense since the above code would fail static checks (List<int> does not have a lengthInBytes member.)

Anyway, I tried doing:

import 'dart:typed_data';
import 'package:http/http.dart' as http;

void main() async {
  var client = http.Client();
  var request = http.Request('GET', Uri.parse('https://www.google.com/'));
  var response = await client.send(request);
  await for (var chunk in response.stream) {
    if (chunk is Uint8List) {
      print('chunk is Uint8List'); // This gets printed.
    }
  }
  client.close();
}

and although the static type of chunk is a List<int>, its actual runtime type is a Uint8List.

(I tried with Dart 2.16.1 stable on linux_x64.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants