[cross_file] Unify XFile interface on web and mobile (#459)
This change changes a couple of methods in the specific implementations of XFile so they implement better the API defined in XFileBase (proper getters instead of public read-only properties)
diff --git a/packages/cross_file/CHANGELOG.md b/packages/cross_file/CHANGELOG.md
index fe50d36..819a96e 100644
--- a/packages/cross_file/CHANGELOG.md
+++ b/packages/cross_file/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.3.1+5
+
+* Unify XFile interface for web and mobile platforms
+
## 0.3.1+4
* The `dart:io` implementation of `saveTo` now does a file copy for path-based
diff --git a/packages/cross_file/lib/src/types/html.dart b/packages/cross_file/lib/src/types/html.dart
index 1e45236..9ed7425 100644
--- a/packages/cross_file/lib/src/types/html.dart
+++ b/packages/cross_file/lib/src/types/html.dart
@@ -23,52 +23,60 @@
/// `name` needs to be passed from the outside, since we only have
/// access to it while we create the ObjectUrl.
XFile(
- this.path, {
- this.mimeType,
+ String path, {
+ String? mimeType,
String? name,
int? length,
Uint8List? bytes,
DateTime? lastModified,
@visibleForTesting CrossFileTestOverrides? overrides,
- }) : _data = bytes,
+ }) : _mimeType = mimeType,
+ _path = path,
+ _data = bytes,
_length = length,
_overrides = overrides,
_lastModified = lastModified ?? DateTime.fromMillisecondsSinceEpoch(0),
- name = name ?? '',
+ _name = name ?? '',
super(path);
/// Construct an CrossFile from its data
XFile.fromData(
Uint8List bytes, {
- this.mimeType,
+ String? mimeType,
String? name,
int? length,
DateTime? lastModified,
String? path,
@visibleForTesting CrossFileTestOverrides? overrides,
- }) : _data = bytes,
+ }) : _mimeType = mimeType,
+ _data = bytes,
_length = length,
_overrides = overrides,
_lastModified = lastModified ?? DateTime.fromMillisecondsSinceEpoch(0),
- name = name ?? '',
+ _name = name ?? '',
super(path) {
if (path == null) {
final Blob blob = (mimeType == null)
? Blob(<dynamic>[bytes])
: Blob(<dynamic>[bytes], mimeType);
- this.path = Url.createObjectUrl(blob);
+ _path = Url.createObjectUrl(blob);
} else {
- this.path = path;
+ _path = path;
}
}
@override
- final String? mimeType;
- @override
- final String name;
- @override
- late String path;
+ String? get mimeType => _mimeType;
+ @override
+ String get name => _name;
+
+ @override
+ String get path => _path;
+
+ final String? _mimeType;
+ final String _name;
+ late String _path;
final Uint8List? _data;
final int? _length;
final DateTime? _lastModified;
diff --git a/packages/cross_file/lib/src/types/io.dart b/packages/cross_file/lib/src/types/io.dart
index 2965cc9..f9fcfea 100644
--- a/packages/cross_file/lib/src/types/io.dart
+++ b/packages/cross_file/lib/src/types/io.dart
@@ -19,12 +19,13 @@
/// [XFile.fromData].
XFile(
String path, {
- this.mimeType,
+ String? mimeType,
String? name,
int? length,
Uint8List? bytes,
DateTime? lastModified,
- }) : _file = File(path),
+ }) : _mimeType = mimeType,
+ _file = File(path),
_bytes = null,
_lastModified = lastModified,
super(path);
@@ -32,12 +33,13 @@
/// Construct an CrossFile from its data
XFile.fromData(
Uint8List bytes, {
- this.mimeType,
+ String? mimeType,
String? path,
String? name,
int? length,
DateTime? lastModified,
- }) : _bytes = bytes,
+ }) : _mimeType = mimeType,
+ _bytes = bytes,
_file = File(path ?? ''),
_length = length,
_lastModified = lastModified,
@@ -48,8 +50,7 @@
}
final File _file;
- @override
- final String? mimeType;
+ final String? _mimeType;
final DateTime? _lastModified;
int? _length;
@@ -75,14 +76,13 @@
}
@override
- String get path {
- return _file.path;
- }
+ String? get mimeType => _mimeType;
@override
- String get name {
- return _file.path.split(Platform.pathSeparator).last;
- }
+ String get path => _file.path;
+
+ @override
+ String get name => _file.path.split(Platform.pathSeparator).last;
@override
Future<int> length() {
diff --git a/packages/cross_file/pubspec.yaml b/packages/cross_file/pubspec.yaml
index 371658c..307418d 100644
--- a/packages/cross_file/pubspec.yaml
+++ b/packages/cross_file/pubspec.yaml
@@ -2,7 +2,7 @@
description: An abstraction to allow working with files across multiple platforms.
repository: https://github.com/flutter/packages/tree/master/packages/cross_file
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+cross_file%22
-version: 0.3.1+4
+version: 0.3.1+5
environment:
sdk: ">=2.12.0 <3.0.0"