Refactor to null aware operators (#308)
Search for any `== null` or `!= null` and where there is a null aware
replacement, use it.
In one place where we could have used `?.` refactor to add a null
assignment within the conditional - it's an unmeasured
micro-optimization but also one which is a common pattern used
elsewhere.
Enable the fixed lint prefer_conditional_assignment
diff --git a/analysis_options.yaml b/analysis_options.yaml
index 1bac99a..543ea34 100644
--- a/analysis_options.yaml
+++ b/analysis_options.yaml
@@ -44,7 +44,7 @@
- package_names
- package_prefixed_library_names
- prefer_adjacent_string_concatenation
- # prefer_conditional_assignment
+ - prefer_conditional_assignment
- prefer_contains
- prefer_equal_for_default_values
- prefer_final_fields
diff --git a/lib/src/browser_client.dart b/lib/src/browser_client.dart
index 601a560..602523c 100644
--- a/lib/src/browser_client.dart
+++ b/lib/src/browser_client.dart
@@ -56,7 +56,7 @@
unawaited(xhr.onLoad.first.then((_) {
// TODO(nweiz): Set the response type to "arraybuffer" when issue 18542
// is fixed.
- var blob = xhr.response == null ? Blob([]) : xhr.response;
+ var blob = xhr.response ?? Blob([]);
var reader = FileReader();
reader.onLoad.first.then((_) {
diff --git a/lib/src/io_client.dart b/lib/src/io_client.dart
index 64579e7..f38a9c4 100644
--- a/lib/src/io_client.dart
+++ b/lib/src/io_client.dart
@@ -34,8 +34,7 @@
var ioRequest = (await _inner.openUrl(request.method, request.url))
..followRedirects = request.followRedirects
..maxRedirects = request.maxRedirects
- ..contentLength =
- request.contentLength == null ? -1 : request.contentLength
+ ..contentLength = (request?.contentLength ?? -1)
..persistentConnection = request.persistentConnection;
request.headers.forEach((name, value) {
ioRequest.headers.set(name, value);
@@ -69,7 +68,9 @@
/// remains unclosed, the Dart process may not terminate.
@override
void close() {
- if (_inner != null) _inner.close(force: true);
- _inner = null;
+ if (_inner != null) {
+ _inner.close(force: true);
+ _inner = null;
+ }
}
}
diff --git a/lib/src/multipart_file.dart b/lib/src/multipart_file.dart
index 406aafe..4f23c78 100644
--- a/lib/src/multipart_file.dart
+++ b/lib/src/multipart_file.dart
@@ -46,9 +46,8 @@
MultipartFile(this.field, Stream<List<int>> stream, this.length,
{this.filename, MediaType contentType})
: this._stream = toByteStream(stream),
- this.contentType = contentType != null
- ? contentType
- : MediaType('application', 'octet-stream');
+ this.contentType =
+ contentType ?? MediaType('application', 'octet-stream');
/// Creates a new [MultipartFile] from a byte array.
///
@@ -69,8 +68,7 @@
/// the future may be inferred from [filename].
factory MultipartFile.fromString(String field, String value,
{String filename, MediaType contentType}) {
- contentType =
- contentType == null ? MediaType('text', 'plain') : contentType;
+ contentType ??= MediaType('text', 'plain');
var encoding = encodingForCharset(contentType.parameters['charset'], utf8);
contentType = contentType.change(parameters: {'charset': encoding.name});
diff --git a/lib/src/multipart_file_io.dart b/lib/src/multipart_file_io.dart
index fb63ab0..a84402f 100644
--- a/lib/src/multipart_file_io.dart
+++ b/lib/src/multipart_file_io.dart
@@ -14,7 +14,7 @@
Future<MultipartFile> multipartFileFromPath(String field, String filePath,
{String filename, MediaType contentType}) async {
- if (filename == null) filename = p.basename(filePath);
+ filename ??= p.basename(filePath);
var file = File(filePath);
var length = await file.length();
var stream = ByteStream(DelegatingStream.typed(file.openRead()));
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index 67aabe0..c29ad64 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -43,18 +43,15 @@
/// [charset].
Encoding encodingForCharset(String charset, [Encoding fallback = latin1]) {
if (charset == null) return fallback;
- var encoding = Encoding.getByName(charset);
- return encoding == null ? fallback : encoding;
+ return Encoding.getByName(charset) ?? fallback;
}
/// Returns the [Encoding] that corresponds to [charset]. Throws a
/// [FormatException] if no [Encoding] was found that corresponds to [charset].
/// [charset] may not be null.
-Encoding requiredEncodingForCharset(String charset) {
- var encoding = Encoding.getByName(charset);
- if (encoding != null) return encoding;
- throw FormatException('Unsupported encoding "$charset".');
-}
+Encoding requiredEncodingForCharset(String charset) =>
+ Encoding.getByName(charset) ??
+ (throw FormatException('Unsupported encoding "$charset".'));
/// A regular expression that matches strings that are composed entirely of
/// ASCII-compatible characters.
diff --git a/test/io/utils.dart b/test/io/utils.dart
index 8ea8f27..9ab4deb 100644
--- a/test/io/utils.dart
+++ b/test/io/utils.dart
@@ -64,13 +64,10 @@
}
ByteStream(request).toBytes().then((requestBodyBytes) {
- var outputEncoding;
var encodingName = request.uri.queryParameters['response-encoding'];
- if (encodingName != null) {
- outputEncoding = requiredEncodingForCharset(encodingName);
- } else {
- outputEncoding = ascii;
- }
+ var outputEncoding = encodingName == null
+ ? ascii
+ : requiredEncodingForCharset(encodingName);
response.headers.contentType =
ContentType('application', 'json', charset: outputEncoding.name);
@@ -79,8 +76,7 @@
var requestBody;
if (requestBodyBytes.isEmpty) {
requestBody = null;
- } else if (request.headers.contentType != null &&
- request.headers.contentType.charset != null) {
+ } else if (request.headers.contentType?.charset != null) {
var encoding =
requiredEncodingForCharset(request.headers.contentType.charset);
requestBody = encoding.decode(requestBodyBytes);