Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 16f3281

Browse files
authored
Reland "[cross_file] Migrate to null-safety. (#3452)" (#3469)
This reverts commit 4aacf97.
1 parent 4aacf97 commit 16f3281

File tree

11 files changed

+99
-95
lines changed

11 files changed

+99
-95
lines changed

packages/cross_file/CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
## 0.3.0-nullsafety
2+
3+
* Migrated package to null-safety.
4+
* **breaking change** According to our unit tests, the API should be backwards-compatible. Some relevant changes were made, however:
5+
* Web: `lastModified` returns the epoch time as a default value, to maintain the `Future<DateTime>` return type (and not `null`)
6+
17
## 0.2.1
28

3-
* Prepare for breaking `package:http` change.
9+
* Prepare for breaking `package:http` change.
410

511
## 0.2.0
612

@@ -12,8 +18,8 @@
1218

1319
## 0.1.0+1
1420

15-
- Update Flutter SDK constraint.
21+
* Update Flutter SDK constraint.
1622

1723
## 0.1.0
1824

19-
- Initial open-source release
25+
* Initial open-source release.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'dart:typed_data';
1515
/// the methods should seem familiar.
1616
abstract class XFileBase {
1717
/// Construct a CrossFile
18-
XFileBase(String path);
18+
XFileBase(String? path);
1919

2020
/// Save the CrossFile at the indicated file path.
2121
Future<void> saveTo(String path) {
@@ -31,19 +31,19 @@ abstract class XFileBase {
3131
/// Accessing the data contained in the picked file by its path
3232
/// is platform-dependant (and won't work on web), so use the
3333
/// byte getters in the CrossFile instance instead.
34-
String get path {
34+
String? get path {
3535
throw UnimplementedError('.path has not been implemented.');
3636
}
3737

3838
/// The name of the file as it was selected by the user in their device.
3939
///
4040
/// Use only for cosmetic reasons, do not try to use this as a path.
41-
String get name {
41+
String? get name {
4242
throw UnimplementedError('.name has not been implemented.');
4343
}
4444

4545
/// For web, it may be necessary for a file to know its MIME type.
46-
String get mimeType {
46+
String? get mimeType {
4747
throw UnimplementedError('.mimeType has not been implemented.');
4848
}
4949

@@ -75,7 +75,7 @@ abstract class XFileBase {
7575
/// If `end` is present, only up to byte-index `end` will be read. Otherwise, until end of file.
7676
///
7777
/// In order to make sure that system resources are freed, the stream must be read to completion or the subscription on the stream must be cancelled.
78-
Stream<Uint8List> openRead([int start, int end]) {
78+
Stream<Uint8List> openRead([int? start, int? end]) {
7979
throw UnimplementedError('openRead() has not been implemented.');
8080
}
8181

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

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'dart:convert';
66
import 'dart:html';
77
import 'dart:typed_data';
88

9-
import 'package:http/http.dart' as http show readBytes;
109
import 'package:meta/meta.dart';
1110

1211
import './base.dart';
@@ -16,16 +15,17 @@ import '../web_helpers/web_helpers.dart';
1615
///
1716
/// It wraps the bytes of a selected file.
1817
class 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
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class XFile extends XFileBase {
2121
/// (like in web)
2222
XFile(
2323
String path, {
24-
String mimeType,
25-
String name,
26-
int length,
27-
Uint8List bytes,
28-
DateTime lastModified,
29-
@visibleForTesting CrossFileTestOverrides overrides,
24+
String? mimeType,
25+
String? name,
26+
int? length,
27+
Uint8List? bytes,
28+
DateTime? lastModified,
29+
@visibleForTesting CrossFileTestOverrides? overrides,
3030
}) : super(path) {
3131
throw UnimplementedError(
3232
'CrossFile is not available in your current platform.');
@@ -35,12 +35,12 @@ class XFile extends XFileBase {
3535
/// Construct a CrossFile object from its data
3636
XFile.fromData(
3737
Uint8List bytes, {
38-
String mimeType,
39-
String name,
40-
int length,
41-
DateTime lastModified,
42-
String path,
43-
@visibleForTesting CrossFileTestOverrides overrides,
38+
String? mimeType,
39+
String? name,
40+
int? length,
41+
DateTime? lastModified,
42+
String? path,
43+
@visibleForTesting CrossFileTestOverrides? overrides,
4444
}) : super(path) {
4545
throw UnimplementedError(
4646
'CrossFile is not available in your current platform.');
@@ -54,5 +54,5 @@ class CrossFileTestOverrides {
5454
dynamic Function(String href, String suggestedName) createAnchorElement;
5555

5656
/// Default constructor for overrides
57-
CrossFileTestOverrides({this.createAnchorElement});
57+
CrossFileTestOverrides({required this.createAnchorElement});
5858
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import './base.dart';
1111
/// A CrossFile backed by a dart:io File.
1212
class XFile extends XFileBase {
1313
final File _file;
14-
final String mimeType;
15-
final DateTime _lastModified;
16-
int _length;
14+
final String? mimeType;
15+
final DateTime? _lastModified;
16+
int? _length;
1717

18-
final Uint8List _bytes;
18+
final Uint8List? _bytes;
1919

2020
/// Construct a CrossFile object backed by a dart:io File.
2121
XFile(
2222
String path, {
2323
this.mimeType,
24-
String name,
25-
int length,
26-
Uint8List bytes,
27-
DateTime lastModified,
24+
String? name,
25+
int? length,
26+
Uint8List? bytes,
27+
DateTime? lastModified,
2828
}) : _file = File(path),
2929
_bytes = null,
3030
_lastModified = lastModified,
@@ -34,10 +34,10 @@ class XFile extends XFileBase {
3434
XFile.fromData(
3535
Uint8List bytes, {
3636
this.mimeType,
37-
String path,
38-
String name,
39-
int length,
40-
DateTime lastModified,
37+
String? path,
38+
String? name,
39+
int? length,
40+
DateTime? lastModified,
4141
}) : _bytes = bytes,
4242
_file = File(path ?? ''),
4343
_length = length,
@@ -84,7 +84,7 @@ class XFile extends XFileBase {
8484
@override
8585
Future<String> readAsString({Encoding encoding = utf8}) {
8686
if (_bytes != null) {
87-
return Future.value(String.fromCharCodes(_bytes));
87+
return Future.value(String.fromCharCodes(_bytes!));
8888
}
8989
return _file.readAsString(encoding: encoding);
9090
}
@@ -97,13 +97,13 @@ class XFile extends XFileBase {
9797
return _file.readAsBytes();
9898
}
9999

100-
Stream<Uint8List> _getBytes(int start, int end) async* {
101-
final bytes = _bytes;
100+
Stream<Uint8List> _getBytes(int? start, int? end) async* {
101+
final bytes = _bytes!;
102102
yield bytes.sublist(start ?? 0, end ?? bytes.length);
103103
}
104104

105105
@override
106-
Stream<Uint8List> openRead([int start, int end]) {
106+
Stream<Uint8List> openRead([int? start, int? end]) {
107107
if (_bytes != null) {
108108
return _getBytes(start, end);
109109
} else {

packages/cross_file/lib/src/web_helpers/web_helpers.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Element ensureInitialized(String id) {
3131
if (target == null) {
3232
final Element targetElement = Element.tag('flt-x-file')..id = id;
3333

34-
querySelector('body').children.add(targetElement);
34+
querySelector('body')!.children.add(targetElement);
3535
target = targetElement;
3636
}
3737
return target;

packages/cross_file/pubspec.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
name: cross_file
22
description: An abstraction to allow working with files across multiple platforms.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/cross_file
4-
version: 0.2.1
4+
version: 0.3.0-nullsafety
55

66
dependencies:
77
flutter:
88
sdk: flutter
9-
http: ^0.12.0+1
10-
meta: ^1.0.5
9+
meta: ^1.3.0-nullsafety.3
1110

1211
dev_dependencies:
1312
flutter_test:
1413
sdk: flutter
15-
pedantic: ^1.8.0
14+
pedantic: ^1.10.0-nullsafety.3
1615

1716
environment:
18-
sdk: ">=2.1.0 <3.0.0"
17+
sdk: ">=2.12.0-0 <3.0.0"
1918
flutter: ">=1.22.0"

0 commit comments

Comments
 (0)