-
Notifications
You must be signed in to change notification settings - Fork 1.7k
File.readAsBytes should return Uint8List #31547
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
Comments
The implementation already returns a |
Awesome. Any reason not to declare that in the API? |
I don't know. /cc @lrhn @floitschG |
Here's the use case: I'm reviewing code that defines a new API for comparing binary images as bytes. The API currently takes |
Also, |
I would recommend changing the API to state that the byte list is a There are two dangers with saying
In the latter case, someone might try to mock the class fail to return a There is one danger with saying I don't think that last danger is an actual problem. That kind of issue only tends to crop up in testing, and performance isn't the same issue there. |
+1. For example, implementing Flutter's import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path/path.dart' as p;
/// A simple implementation of [AssetBundle] that reads files from an asset dir.
///
/// This is meant to be similar to the default [rootBundle] for testing.
class DiskAssetBundle extends CachingAssetBundle {
final Directory _assetPath;
DiskAssetBundle([Directory assetPath])
: _assetPath = assetPath ??
Directory(
p.join(Directory.current.path, 'assets'),
),
assert(_assetPath.existsSync(), 'Asset folder expected');
@override
Future<ByteData> load(String key) {
return File(p.join(_path, key))
.readAsBytes()
.then((data) => ByteData.view(data as ByteBuffer));
}
} |
+1! Whenever I think about binary data "int" and "Uint8" are not the same animal. The semantics of Dart code should make it clear that we're talking about Uint8 all the way. |
It seems like everybody agrees that this should be done, and nobody's given any reasons not to do it nor described any potential problems. Is there anything holding this up? |
Breaking change request: #36900 |
These methods all were returning Uint8List, yet they were only declared to return List<int>. This forced callers to either defensively wrap the return values in Uint8List, or to assume the contravariant return value: * Utf8Codec.encode() * BytesBuilder.takeBytes() * BytesBuilder.toBytes() * File.readAsBytes() * File.readAsBytesSync() * RandomAccessFile.read() * RandomAccessFile.readSync() * Uint8List.sublist() Since it's related, this change also updates the following sublist() methods to declare that they return the a sublist of the same type as the source list: * Int8List * Uint8ClampedList * Int16List * Uint16List * Int32List * Uint32List * Int64List * Uint64List * Float32List * Float64List * Float32x4List * Int32x4List * Float64x2List Bug: #36900 Bug: #31547 Bug: #27818 Bug: #35521 Change-Id: Ic3bc1db0d64de36fb68b1d8d98037eed1464f978 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101742 Commit-Queue: Todd Volkert <[email protected]> Reviewed-by: Lasse R.H. Nielsen <[email protected]>
This is done now. |
File.readAsBytes
(and the sync version as well) should returnUint8List
rather thanList<int>
. This is a backwards-compatible change sinceUint8List
implementsList<int>
.@zanderso
The text was updated successfully, but these errors were encountered: