Skip to content

Commit 6d180d9

Browse files
[cross_file] always set browser blob when constructing XFile from data (#8611)
This PR fixes a bug where files created with `XFile.fromData()` would ignore the provided bytes if the path is present, too. Now if the bytes and the path are both provided, the content is read from the bytes directly. Fixes flutter/flutter#163044
1 parent 41c6b3d commit 6d180d9

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

packages/cross_file/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 0.3.5
22

3+
* Fixes a bug where the bytes of an XFile, that is created using the `XFile.fromData` constructor, are ignored on web.
34
* Updates minimum supported SDK version to Flutter 3.29/Dart 3.7.
45

56
## 0.3.4+2

packages/cross_file/lib/src/types/html.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ class XFile extends XFileBase {
6464
_lastModified = lastModified ?? DateTime.fromMillisecondsSinceEpoch(0),
6565
_name = name ?? '',
6666
super(path) {
67-
if (path == null) {
68-
_browserBlob = _createBlobFromBytes(bytes, mimeType);
69-
_path = URL.createObjectURL(_browserBlob!);
70-
} else {
71-
_path = path;
72-
}
67+
_browserBlob = _createBlobFromBytes(bytes, mimeType);
68+
_path = URL.createObjectURL(_browserBlob!);
7369
}
7470

7571
// Initializes a Blob from a bunch of `bytes` and an optional `mimeType`.

packages/cross_file/lib/src/types/interface.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class XFile extends XFileBase {
3535
);
3636
}
3737

38-
/// Construct a CrossFile object from its data
38+
/// Construct a CrossFile object from its data.
39+
///
40+
/// On the web, the [path] is ignored if the [bytes] are provided,
41+
/// as the underlying Blob URL is used as the path.
3942
XFile.fromData(
4043
Uint8List bytes, {
4144
String? mimeType,

packages/cross_file/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: cross_file
22
description: An abstraction to allow working with files across multiple platforms.
33
repository: https://github.com/flutter/packages/tree/main/packages/cross_file
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+cross_file%22
5-
version: 0.3.4+2
5+
version: 0.3.5
66

77
environment:
88
sdk: ^3.7.0

packages/cross_file/test/x_file_html_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ void main() {
6363
test('Stream can be sliced', () async {
6464
expect(await file.openRead(2, 5).first, equals(bytes.sublist(2, 5)));
6565
});
66+
67+
test('Prefers local bytes over path if both are provided', () async {
68+
const String text = 'Hello World';
69+
const String path = 'test/x_file_html_test.dart';
70+
71+
final XFile file = XFile.fromData(
72+
utf8.encode(text),
73+
path: path,
74+
name: 'x_file_html_test.dart',
75+
length: text.length,
76+
mimeType: 'text/plain',
77+
lastModified: DateTime.now(),
78+
);
79+
80+
expect(file.path, isNot(equals(path)));
81+
expect(file.path.startsWith('blob:'), isTrue);
82+
expect(await file.readAsString(), equals(text));
83+
});
6684
});
6785

6886
group('Blob backend', () {

0 commit comments

Comments
 (0)