Make analyzer happy. Tests pass.
diff --git a/packages/cross_file/lib/src/types/base.dart b/packages/cross_file/lib/src/types/base.dart
index 4522b73..98c2f8c 100644
--- a/packages/cross_file/lib/src/types/base.dart
+++ b/packages/cross_file/lib/src/types/base.dart
@@ -15,6 +15,7 @@
/// the methods should seem familiar.
abstract class XFileBase {
/// Construct a CrossFile
+ // ignore: avoid_unused_constructor_parameters
XFileBase(String? path);
/// Save the CrossFile at the indicated file path.
diff --git a/packages/cross_file/lib/src/types/html.dart b/packages/cross_file/lib/src/types/html.dart
index 203ab5d..ef69af5 100644
--- a/packages/cross_file/lib/src/types/html.dart
+++ b/packages/cross_file/lib/src/types/html.dart
@@ -8,27 +8,13 @@
import 'package:meta/meta.dart';
-import './base.dart';
import '../web_helpers/web_helpers.dart';
+import './base.dart';
/// A CrossFile that works on web.
///
/// It wraps the bytes of a selected file.
class XFile extends XFileBase {
- late String path;
-
- final String? mimeType;
- final Uint8List? _data;
- final int? _length;
- final String name;
- final DateTime? _lastModified;
-
- late Element _target;
-
- final CrossFileTestOverrides? _overrides;
-
- bool get _hasTestOverrides => _overrides != null;
-
/// Construct a CrossFile object from its ObjectUrl.
///
/// Optionally, this can be initialized with `bytes` and `length`
@@ -67,7 +53,9 @@
name = name ?? '',
super(path) {
if (path == null) {
- final blob = (mimeType == null) ? Blob([bytes]) : Blob([bytes], mimeType);
+ final Blob blob = (mimeType == null)
+ ? Blob(<dynamic>[bytes])
+ : Blob(<dynamic>[bytes], mimeType);
this.path = Url.createObjectUrl(blob);
} else {
this.path = path;
@@ -75,15 +63,33 @@
}
@override
- Future<DateTime> lastModified() async => Future.value(_lastModified);
+ final String? mimeType;
+ @override
+ final String name;
+ @override
+ late String path;
+
+ final Uint8List? _data;
+ final int? _length;
+ final DateTime? _lastModified;
+
+ late Element _target;
+
+ final CrossFileTestOverrides? _overrides;
+
+ bool get _hasTestOverrides => _overrides != null;
+
+ @override
+ Future<DateTime> lastModified() async =>
+ Future<DateTime>.value(_lastModified);
Future<Uint8List> get _bytes async {
if (_data != null) {
- return Future.value(UnmodifiableUint8ListView(_data!));
+ return Future<Uint8List>.value(UnmodifiableUint8ListView(_data!));
}
// We can force 'response' to be a byte buffer by passing responseType:
- ByteBuffer? response =
+ final ByteBuffer? response =
(await HttpRequest.request(path, responseType: 'arraybuffer')).response;
return response?.asUint8List() ?? Uint8List(0);
@@ -98,16 +104,18 @@
}
@override
- Future<Uint8List> readAsBytes() async => Future.value(await _bytes);
+ Future<Uint8List> readAsBytes() async =>
+ Future<Uint8List>.value(await _bytes);
@override
Stream<Uint8List> openRead([int? start, int? end]) async* {
- final bytes = await _bytes;
+ final Uint8List bytes = await _bytes;
yield bytes.sublist(start ?? 0, end ?? bytes.length);
}
/// Saves the data of this CrossFile at the location indicated by path.
/// For the web implementation, the path variable is ignored.
+ @override
Future<void> saveTo(String path) async {
// Create a DOM container where we can host the anchor.
_target = ensureInitialized('__x_file_dom_element');
@@ -115,8 +123,8 @@
// Create an <a> tag with the appropriate download attributes and click it
// May be overridden with CrossFileTestOverrides
final AnchorElement element = _hasTestOverrides
- ? _overrides!.createAnchorElement(this.path, this.name) as AnchorElement
- : createAnchorElement(this.path, this.name);
+ ? _overrides!.createAnchorElement(this.path, name) as AnchorElement
+ : createAnchorElement(this.path, name);
// Clear the children in our container so we can add an element to click
_target.children.clear();
@@ -127,9 +135,9 @@
/// Overrides some functions to allow testing
@visibleForTesting
class CrossFileTestOverrides {
- /// For overriding the creation of the file input element.
- Element Function(String href, String suggestedName) createAnchorElement;
-
/// Default constructor for overrides
CrossFileTestOverrides({required this.createAnchorElement});
+
+ /// For overriding the creation of the file input element.
+ Element Function(String href, String suggestedName) createAnchorElement;
}
diff --git a/packages/cross_file/lib/src/types/interface.dart b/packages/cross_file/lib/src/types/interface.dart
index 122f3d1..91afac5 100644
--- a/packages/cross_file/lib/src/types/interface.dart
+++ b/packages/cross_file/lib/src/types/interface.dart
@@ -7,6 +7,8 @@
import './base.dart';
+// ignore_for_file: avoid_unused_constructor_parameters
+
/// A CrossFile is a cross-platform, simplified File abstraction.
///
/// It wraps the bytes of a selected file, and its (platform-dependant) path.
@@ -50,9 +52,9 @@
/// Overrides some functions of CrossFile for testing purposes
@visibleForTesting
class CrossFileTestOverrides {
- /// For overriding the creation of the file input element.
- dynamic Function(String href, String suggestedName) createAnchorElement;
-
/// Default constructor for overrides
CrossFileTestOverrides({required this.createAnchorElement});
+
+ /// For overriding the creation of the file input element.
+ dynamic Function(String href, String suggestedName) createAnchorElement;
}
diff --git a/packages/cross_file/lib/src/types/io.dart b/packages/cross_file/lib/src/types/io.dart
index 6eafaf0..6d649ce 100644
--- a/packages/cross_file/lib/src/types/io.dart
+++ b/packages/cross_file/lib/src/types/io.dart
@@ -8,15 +8,10 @@
import './base.dart';
+// ignore_for_file: avoid_unused_constructor_parameters
+
/// A CrossFile backed by a dart:io File.
class XFile extends XFileBase {
- final File _file;
- final String? mimeType;
- final DateTime? _lastModified;
- int? _length;
-
- final Uint8List? _bytes;
-
/// Construct a CrossFile object backed by a dart:io File.
XFile(
String path, {
@@ -48,17 +43,26 @@
}
}
+ final File _file;
+ @override
+ final String? mimeType;
+ final DateTime? _lastModified;
+ int? _length;
+
+ final Uint8List? _bytes;
+
@override
Future<DateTime> lastModified() {
if (_lastModified != null) {
- return Future.value(_lastModified);
+ return Future<DateTime>.value(_lastModified);
}
+ // ignore: avoid_slow_async_io
return _file.lastModified();
}
@override
Future<void> saveTo(String path) async {
- File fileToSave = File(path);
+ final File fileToSave = File(path);
await fileToSave.writeAsBytes(_bytes ?? (await readAsBytes()));
await fileToSave.create();
}
@@ -76,7 +80,7 @@
@override
Future<int> length() {
if (_length != null) {
- return Future.value(_length);
+ return Future<int>.value(_length);
}
return _file.length();
}
@@ -84,7 +88,7 @@
@override
Future<String> readAsString({Encoding encoding = utf8}) {
if (_bytes != null) {
- return Future.value(String.fromCharCodes(_bytes!));
+ return Future<String>.value(String.fromCharCodes(_bytes!));
}
return _file.readAsString(encoding: encoding);
}
@@ -92,13 +96,13 @@
@override
Future<Uint8List> readAsBytes() {
if (_bytes != null) {
- return Future.value(_bytes);
+ return Future<Uint8List>.value(_bytes);
}
return _file.readAsBytes();
}
Stream<Uint8List> _getBytes(int? start, int? end) async* {
- final bytes = _bytes!;
+ final Uint8List bytes = _bytes!;
yield bytes.sublist(start ?? 0, end ?? bytes.length);
}
@@ -109,7 +113,7 @@
} else {
return _file
.openRead(start ?? 0, end)
- .map((chunk) => Uint8List.fromList(chunk));
+ .map((List<int> chunk) => Uint8List.fromList(chunk));
}
}
}
diff --git a/packages/cross_file/lib/src/web_helpers/web_helpers.dart b/packages/cross_file/lib/src/web_helpers/web_helpers.dart
index a963e99..9440d8a 100644
--- a/packages/cross_file/lib/src/web_helpers/web_helpers.dart
+++ b/packages/cross_file/lib/src/web_helpers/web_helpers.dart
@@ -5,8 +5,8 @@
import 'dart:html';
/// Create anchor element with download attribute
-AnchorElement createAnchorElement(String href, String suggestedName) {
- final element = AnchorElement(href: href);
+AnchorElement createAnchorElement(String href, String? suggestedName) {
+ final AnchorElement element = AnchorElement(href: href);
if (suggestedName == null) {
element.download = 'download';
@@ -27,7 +27,7 @@
/// Initializes a DOM container where we can host elements.
Element ensureInitialized(String id) {
- var target = querySelector('#${id}');
+ Element? target = querySelector('#$id');
if (target == null) {
final Element targetElement = Element.tag('flt-x-file')..id = id;
diff --git a/packages/cross_file/test/x_file_html_test.dart b/packages/cross_file/test/x_file_html_test.dart
index a271aa1..43740f0 100644
--- a/packages/cross_file/test/x_file_html_test.dart
+++ b/packages/cross_file/test/x_file_html_test.dart
@@ -11,14 +11,14 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:cross_file/cross_file.dart';
-final String expectedStringContents = 'Hello, world!';
+const String expectedStringContents = 'Hello, world!';
final Uint8List bytes = Uint8List.fromList(utf8.encode(expectedStringContents));
-final html.File textFile = html.File([bytes], 'hello.txt');
+final html.File textFile = html.File(<Object>[bytes], 'hello.txt');
final String textFileUrl = html.Url.createObjectUrl(textFile);
void main() {
group('Create with an objectUrl', () {
- final file = XFile(textFileUrl);
+ final XFile file = XFile(textFileUrl);
test('Can be read as a string', () async {
expect(await file.readAsString(), equals(expectedStringContents));
@@ -37,7 +37,7 @@
});
group('Create from data', () {
- final file = XFile.fromData(bytes);
+ final XFile file = XFile.fromData(bytes);
test('Can be read as a string', () async {
expect(await file.readAsString(), equals(expectedStringContents));
@@ -56,28 +56,30 @@
});
group('saveTo(..)', () {
- final String CrossFileDomElementId = '__x_file_dom_element';
+ const String crossFileDomElementId = '__x_file_dom_element';
group('CrossFile saveTo(..)', () {
test('creates a DOM container', () async {
- XFile file = XFile.fromData(bytes);
+ final XFile file = XFile.fromData(bytes);
await file.saveTo('');
- final container = html.querySelector('#${CrossFileDomElementId}');
+ final html.Element? container =
+ html.querySelector('#$crossFileDomElementId');
expect(container, isNotNull);
});
test('create anchor element', () async {
- XFile file = XFile.fromData(bytes, name: textFile.name);
+ final XFile file = XFile.fromData(bytes, name: textFile.name);
await file.saveTo('path');
- final container = html.querySelector('#${CrossFileDomElementId}');
- final html.AnchorElement element =
- container?.children.firstWhere((element) => element.tagName == 'A')
- as html.AnchorElement;
+ final html.Element? container =
+ html.querySelector('#$crossFileDomElementId');
+ final html.AnchorElement element = container?.children
+ .firstWhere((html.Element element) => element.tagName == 'A')
+ as html.AnchorElement;
// if element is not found, the `firstWhere` call will throw StateError.
expect(element.href, file.path);
@@ -85,17 +87,17 @@
});
test('anchor element is clicked', () async {
- final mockAnchor = html.AnchorElement();
+ final html.AnchorElement mockAnchor = html.AnchorElement();
- CrossFileTestOverrides overrides = CrossFileTestOverrides(
+ final CrossFileTestOverrides overrides = CrossFileTestOverrides(
createAnchorElement: (_, __) => mockAnchor,
);
- XFile file =
+ final XFile file =
XFile.fromData(bytes, name: textFile.name, overrides: overrides);
bool clicked = false;
- mockAnchor.onClick.listen((event) => clicked = true);
+ mockAnchor.onClick.listen((html.MouseEvent event) => clicked = true);
await file.saveTo('path');
diff --git a/packages/cross_file/test/x_file_io_test.dart b/packages/cross_file/test/x_file_io_test.dart
index 94ac81c..a8edbe5 100644
--- a/packages/cross_file/test/x_file_io_test.dart
+++ b/packages/cross_file/test/x_file_io_test.dart
@@ -11,10 +11,10 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:cross_file/cross_file.dart';
-final pathPrefix =
+final String pathPrefix =
Directory.current.path.endsWith('test') ? './assets/' : './test/assets/';
-final path = pathPrefix + 'hello.txt';
-final String expectedStringContents = 'Hello, world!';
+final String path = pathPrefix + 'hello.txt';
+const String expectedStringContents = 'Hello, world!';
final Uint8List bytes = Uint8List.fromList(utf8.encode(expectedStringContents));
final File textFile = File(path);
final String textFilePath = textFile.path;
@@ -39,13 +39,13 @@
});
test('saveTo(..) creates file', () async {
- File removeBeforeTest = File(pathPrefix + 'newFilePath.txt');
+ final File removeBeforeTest = File(pathPrefix + 'newFilePath.txt');
if (removeBeforeTest.existsSync()) {
await removeBeforeTest.delete();
}
await file.saveTo(pathPrefix + 'newFilePath.txt');
- File newFile = File(pathPrefix + 'newFilePath.txt');
+ final File newFile = File(pathPrefix + 'newFilePath.txt');
expect(newFile.existsSync(), isTrue);
expect(newFile.readAsStringSync(), 'Hello, world!');
@@ -55,7 +55,7 @@
});
group('Create with data', () {
- final file = XFile.fromData(bytes);
+ final XFile file = XFile.fromData(bytes);
test('Can be read as a string', () async {
expect(await file.readAsString(), equals(expectedStringContents));
@@ -73,13 +73,13 @@
});
test('Function saveTo(..) creates file', () async {
- File removeBeforeTest = File(pathPrefix + 'newFileData.txt');
+ final File removeBeforeTest = File(pathPrefix + 'newFileData.txt');
if (removeBeforeTest.existsSync()) {
await removeBeforeTest.delete();
}
await file.saveTo(pathPrefix + 'newFileData.txt');
- File newFile = File(pathPrefix + 'newFileData.txt');
+ final File newFile = File(pathPrefix + 'newFileData.txt');
expect(newFile.existsSync(), isTrue);
expect(newFile.readAsStringSync(), 'Hello, world!');