@@ -6,6 +6,7 @@ import 'dart:convert';
66import 'dart:html' ;
77import 'dart:typed_data' ;
88
9+ import 'package:http/http.dart' as http show readBytes;
910import 'package:meta/meta.dart' ;
1011
1112import './base.dart' ;
@@ -15,17 +16,16 @@ import '../web_helpers/web_helpers.dart';
1516///
1617/// It wraps the bytes of a selected file.
1718class XFile extends XFileBase {
18- late String path;
19+ String path;
1920
20- final String ? mimeType;
21- final Uint8List ? _data;
22- final int ? _length;
21+ final String mimeType;
22+ final Uint8List _data;
23+ final int _length;
2324 final String name;
24- final DateTime ? _lastModified;
25+ final DateTime _lastModified;
26+ Element _target;
2527
26- late Element _target;
27-
28- final CrossFileTestOverrides ? _overrides;
28+ final CrossFileTestOverrides _overrides;
2929
3030 bool get _hasTestOverrides => _overrides != null ;
3131
@@ -39,69 +39,69 @@ class XFile extends XFileBase {
3939 XFile (
4040 this .path, {
4141 this .mimeType,
42- String ? name,
43- int ? length,
44- Uint8List ? bytes,
45- DateTime ? lastModified,
46- @visibleForTesting CrossFileTestOverrides ? overrides,
42+ this . name,
43+ int length,
44+ Uint8List bytes,
45+ DateTime lastModified,
46+ @visibleForTesting CrossFileTestOverrides overrides,
4747 }) : _data = bytes,
4848 _length = length,
4949 _overrides = overrides,
50- _lastModified = lastModified ?? DateTime .fromMillisecondsSinceEpoch (0 ),
51- name = name ?? '' ,
50+ _lastModified = lastModified,
5251 super (path);
5352
5453 /// Construct an CrossFile from its data
5554 XFile .fromData (
5655 Uint8List bytes, {
5756 this .mimeType,
58- String ? name,
59- int ? length,
60- DateTime ? lastModified,
61- String ? path,
62- @visibleForTesting CrossFileTestOverrides ? overrides,
57+ this . name,
58+ int length,
59+ DateTime lastModified,
60+ this . path,
61+ @visibleForTesting CrossFileTestOverrides overrides,
6362 }) : _data = bytes,
6463 _length = length,
6564 _overrides = overrides,
66- _lastModified = lastModified ?? DateTime .fromMillisecondsSinceEpoch (0 ),
67- name = name ?? '' ,
65+ _lastModified = lastModified,
6866 super (path) {
6967 if (path == null ) {
7068 final blob = (mimeType == null ) ? Blob ([bytes]) : Blob ([bytes], mimeType);
7169 this .path = Url .createObjectUrl (blob);
72- } else {
73- this .path = path;
7470 }
7571 }
7672
7773 @override
78- Future <DateTime > lastModified () async => Future .value (_lastModified);
74+ Future <DateTime > lastModified () async {
75+ if (_lastModified != null ) {
76+ return Future .value (_lastModified);
77+ }
78+ return null ;
79+ }
7980
8081 Future <Uint8List > get _bytes async {
8182 if (_data != null ) {
82- return Future .value (UnmodifiableUint8ListView (_data! ));
83+ return Future .value (UnmodifiableUint8ListView (_data));
8384 }
84-
85- // We can force 'response' to be a byte buffer by passing responseType:
86- ByteBuffer ? response =
87- (await HttpRequest .request (path, responseType: 'arraybuffer' )).response;
88-
89- return response? .asUint8List () ?? Uint8List (0 );
85+ return http.readBytes (Uri .parse (path));
9086 }
9187
9288 @override
93- Future <int > length () async => _length ?? (await _bytes).length;
89+ Future <int > length () async {
90+ return _length ?? (await _bytes).length;
91+ }
9492
9593 @override
9694 Future <String > readAsString ({Encoding encoding = utf8}) async {
9795 return encoding.decode (await _bytes);
9896 }
9997
10098 @override
101- Future <Uint8List > readAsBytes () async => Future .value (await _bytes);
99+ Future <Uint8List > readAsBytes () async {
100+ return Future .value (await _bytes);
101+ }
102102
103103 @override
104- Stream <Uint8List > openRead ([int ? start, int ? end]) async * {
104+ Stream <Uint8List > openRead ([int start, int end]) async * {
105105 final bytes = await _bytes;
106106 yield bytes.sublist (start ?? 0 , end ?? bytes.length);
107107 }
@@ -114,9 +114,10 @@ class XFile extends XFileBase {
114114
115115 // Create an <a> tag with the appropriate download attributes and click it
116116 // May be overridden with CrossFileTestOverrides
117- final AnchorElement element = _hasTestOverrides
118- ? _overrides! .createAnchorElement (this .path, this .name) as AnchorElement
119- : createAnchorElement (this .path, this .name);
117+ final AnchorElement element =
118+ (_hasTestOverrides && _overrides.createAnchorElement != null )
119+ ? _overrides.createAnchorElement (this .path, this .name)
120+ : createAnchorElement (this .path, this .name);
120121
121122 // Clear the children in our container so we can add an element to click
122123 _target.children.clear ();
@@ -131,5 +132,5 @@ class CrossFileTestOverrides {
131132 Element Function (String href, String suggestedName) createAnchorElement;
132133
133134 /// Default constructor for overrides
134- CrossFileTestOverrides ({required this .createAnchorElement});
135+ CrossFileTestOverrides ({this .createAnchorElement});
135136}
0 commit comments