Skip to content

Commit 4aacf97

Browse files
authored
Revert "[cross_file] Migrate to null-safety. (flutter#3452)" (flutter#3468)
This reverts commit ca99211.
1 parent 98d87d0 commit 4aacf97

File tree

11 files changed

+95
-99
lines changed

11 files changed

+95
-99
lines changed

packages/cross_file/CHANGELOG.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
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-
71
## 0.2.1
82

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

115
## 0.2.0
126

@@ -18,8 +12,8 @@
1812

1913
## 0.1.0+1
2014

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

2317
## 0.1.0
2418

25-
* Initial open-source release.
19+
- 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: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:convert';
66
import 'dart:html';
77
import 'dart:typed_data';
88

9+
import 'package:http/http.dart' as http show readBytes;
910
import 'package:meta/meta.dart';
1011

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

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({required this.createAnchorElement});
57+
CrossFileTestOverrides({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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
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.3.0-nullsafety
4+
version: 0.2.1
55

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

1112
dev_dependencies:
1213
flutter_test:
1314
sdk: flutter
14-
pedantic: ^1.10.0-nullsafety.3
15+
pedantic: ^1.8.0
1516

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

0 commit comments

Comments
 (0)