@@ -6,7 +6,6 @@ import 'dart:convert';
66import 'dart:html' ;
77import 'dart:typed_data' ;
88
9- import 'package:http/http.dart' as http show readBytes;
109import 'package:meta/meta.dart' ;
1110
1211import './base.dart' ;
@@ -16,16 +15,17 @@ import '../web_helpers/web_helpers.dart';
1615///
1716/// It wraps the bytes of a selected file.
1817class XFile extends XFileBase {
19- String path;
18+ late String path;
2019
21- final String mimeType;
22- final Uint8List _data;
23- final int _length;
20+ final String ? mimeType;
21+ final Uint8List ? _data;
22+ final int ? _length;
2423 final String name;
25- final DateTime _lastModified;
26- Element _target;
24+ final DateTime ? _lastModified;
2725
28- final CrossFileTestOverrides _overrides;
26+ late Element _target;
27+
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- this . name,
43- int length,
44- Uint8List bytes,
45- DateTime lastModified,
46- @visibleForTesting CrossFileTestOverrides overrides,
42+ String ? 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,
50+ _lastModified = lastModified ?? DateTime .fromMillisecondsSinceEpoch (0 ),
51+ name = name ?? '' ,
5152 super (path);
5253
5354 /// Construct an CrossFile from its data
5455 XFile .fromData (
5556 Uint8List bytes, {
5657 this .mimeType,
57- this . name,
58- int length,
59- DateTime lastModified,
60- this . path,
61- @visibleForTesting CrossFileTestOverrides overrides,
58+ String ? name,
59+ int ? length,
60+ DateTime ? lastModified,
61+ String ? path,
62+ @visibleForTesting CrossFileTestOverrides ? overrides,
6263 }) : _data = bytes,
6364 _length = length,
6465 _overrides = overrides,
65- _lastModified = lastModified,
66+ _lastModified = lastModified ?? DateTime .fromMillisecondsSinceEpoch (0 ),
67+ name = name ?? '' ,
6668 super (path) {
6769 if (path == null ) {
6870 final blob = (mimeType == null ) ? Blob ([bytes]) : Blob ([bytes], mimeType);
6971 this .path = Url .createObjectUrl (blob);
72+ } else {
73+ this .path = path;
7074 }
7175 }
7276
7377 @override
74- Future <DateTime > lastModified () async {
75- if (_lastModified != null ) {
76- return Future .value (_lastModified);
77- }
78- return null ;
79- }
78+ Future <DateTime > lastModified () async => Future .value (_lastModified);
8079
8180 Future <Uint8List > get _bytes async {
8281 if (_data != null ) {
83- return Future .value (UnmodifiableUint8ListView (_data));
82+ return Future .value (UnmodifiableUint8ListView (_data! ));
8483 }
85- return http.readBytes (Uri .parse (path));
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 );
8690 }
8791
8892 @override
89- Future <int > length () async {
90- return _length ?? (await _bytes).length;
91- }
93+ Future <int > length () async => _length ?? (await _bytes).length;
9294
9395 @override
9496 Future <String > readAsString ({Encoding encoding = utf8}) async {
9597 return encoding.decode (await _bytes);
9698 }
9799
98100 @override
99- Future <Uint8List > readAsBytes () async {
100- return Future .value (await _bytes);
101- }
101+ Future <Uint8List > readAsBytes () async => Future .value (await _bytes);
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,10 +114,9 @@ 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 =
118- (_hasTestOverrides && _overrides.createAnchorElement != null )
119- ? _overrides.createAnchorElement (this .path, this .name)
120- : createAnchorElement (this .path, this .name);
117+ final AnchorElement element = _hasTestOverrides
118+ ? _overrides! .createAnchorElement (this .path, this .name) as AnchorElement
119+ : createAnchorElement (this .path, this .name);
121120
122121 // Clear the children in our container so we can add an element to click
123122 _target.children.clear ();
@@ -132,5 +131,5 @@ class CrossFileTestOverrides {
132131 Element Function (String href, String suggestedName) createAnchorElement;
133132
134133 /// Default constructor for overrides
135- CrossFileTestOverrides ({this .createAnchorElement});
134+ CrossFileTestOverrides ({required this .createAnchorElement});
136135}
0 commit comments