Skip to content

Commit e3351b3

Browse files
authored
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
1 parent eece4c9 commit e3351b3

File tree

7 files changed

+19
-27
lines changed

7 files changed

+19
-27
lines changed

analysis_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ linter:
4444
- package_names
4545
- package_prefixed_library_names
4646
- prefer_adjacent_string_concatenation
47-
# prefer_conditional_assignment
47+
- prefer_conditional_assignment
4848
- prefer_contains
4949
- prefer_equal_for_default_values
5050
- prefer_final_fields

lib/src/browser_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class BrowserClient extends BaseClient {
5656
unawaited(xhr.onLoad.first.then((_) {
5757
// TODO(nweiz): Set the response type to "arraybuffer" when issue 18542
5858
// is fixed.
59-
var blob = xhr.response == null ? Blob([]) : xhr.response;
59+
var blob = xhr.response ?? Blob([]);
6060
var reader = FileReader();
6161

6262
reader.onLoad.first.then((_) {

lib/src/io_client.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class IOClient extends BaseClient {
3434
var ioRequest = (await _inner.openUrl(request.method, request.url))
3535
..followRedirects = request.followRedirects
3636
..maxRedirects = request.maxRedirects
37-
..contentLength =
38-
request.contentLength == null ? -1 : request.contentLength
37+
..contentLength = (request?.contentLength ?? -1)
3938
..persistentConnection = request.persistentConnection;
4039
request.headers.forEach((name, value) {
4140
ioRequest.headers.set(name, value);
@@ -69,7 +68,9 @@ class IOClient extends BaseClient {
6968
/// remains unclosed, the Dart process may not terminate.
7069
@override
7170
void close() {
72-
if (_inner != null) _inner.close(force: true);
73-
_inner = null;
71+
if (_inner != null) {
72+
_inner.close(force: true);
73+
_inner = null;
74+
}
7475
}
7576
}

lib/src/multipart_file.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ class MultipartFile {
4646
MultipartFile(this.field, Stream<List<int>> stream, this.length,
4747
{this.filename, MediaType contentType})
4848
: this._stream = toByteStream(stream),
49-
this.contentType = contentType != null
50-
? contentType
51-
: MediaType('application', 'octet-stream');
49+
this.contentType =
50+
contentType ?? MediaType('application', 'octet-stream');
5251

5352
/// Creates a new [MultipartFile] from a byte array.
5453
///
@@ -69,8 +68,7 @@ class MultipartFile {
6968
/// the future may be inferred from [filename].
7069
factory MultipartFile.fromString(String field, String value,
7170
{String filename, MediaType contentType}) {
72-
contentType =
73-
contentType == null ? MediaType('text', 'plain') : contentType;
71+
contentType ??= MediaType('text', 'plain');
7472
var encoding = encodingForCharset(contentType.parameters['charset'], utf8);
7573
contentType = contentType.change(parameters: {'charset': encoding.name});
7674

lib/src/multipart_file_io.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'multipart_file.dart';
1414

1515
Future<MultipartFile> multipartFileFromPath(String field, String filePath,
1616
{String filename, MediaType contentType}) async {
17-
if (filename == null) filename = p.basename(filePath);
17+
filename ??= p.basename(filePath);
1818
var file = File(filePath);
1919
var length = await file.length();
2020
var stream = ByteStream(DelegatingStream.typed(file.openRead()));

lib/src/utils.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,15 @@ List<String> split1(String toSplit, String pattern) {
4343
/// [charset].
4444
Encoding encodingForCharset(String charset, [Encoding fallback = latin1]) {
4545
if (charset == null) return fallback;
46-
var encoding = Encoding.getByName(charset);
47-
return encoding == null ? fallback : encoding;
46+
return Encoding.getByName(charset) ?? fallback;
4847
}
4948

5049
/// Returns the [Encoding] that corresponds to [charset]. Throws a
5150
/// [FormatException] if no [Encoding] was found that corresponds to [charset].
5251
/// [charset] may not be null.
53-
Encoding requiredEncodingForCharset(String charset) {
54-
var encoding = Encoding.getByName(charset);
55-
if (encoding != null) return encoding;
56-
throw FormatException('Unsupported encoding "$charset".');
57-
}
52+
Encoding requiredEncodingForCharset(String charset) =>
53+
Encoding.getByName(charset) ??
54+
(throw FormatException('Unsupported encoding "$charset".'));
5855

5956
/// A regular expression that matches strings that are composed entirely of
6057
/// ASCII-compatible characters.

test/io/utils.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,10 @@ Future startServer() {
6464
}
6565

6666
ByteStream(request).toBytes().then((requestBodyBytes) {
67-
var outputEncoding;
6867
var encodingName = request.uri.queryParameters['response-encoding'];
69-
if (encodingName != null) {
70-
outputEncoding = requiredEncodingForCharset(encodingName);
71-
} else {
72-
outputEncoding = ascii;
73-
}
68+
var outputEncoding = encodingName == null
69+
? ascii
70+
: requiredEncodingForCharset(encodingName);
7471

7572
response.headers.contentType =
7673
ContentType('application', 'json', charset: outputEncoding.name);
@@ -79,8 +76,7 @@ Future startServer() {
7976
var requestBody;
8077
if (requestBodyBytes.isEmpty) {
8178
requestBody = null;
82-
} else if (request.headers.contentType != null &&
83-
request.headers.contentType.charset != null) {
79+
} else if (request.headers.contentType?.charset != null) {
8480
var encoding =
8581
requiredEncodingForCharset(request.headers.contentType.charset);
8682
requestBody = encoding.decode(requestBodyBytes);

0 commit comments

Comments
 (0)