From 0397e890be63f17108b8b38dd7bfb930308639dc Mon Sep 17 00:00:00 2001 From: Nate Wilson Date: Mon, 5 Aug 2024 19:54:52 -0600 Subject: [PATCH] Implement `on` clauses (#152706) This pull request removes an `// ignore: avoid_catches_without_on_clauses` comment. > [!NOTE] > Diffs are super tiny if you do "hide whitespace"! --- .../material/autocomplete/autocomplete.3.dart | 7 ++--- .../material/autocomplete/autocomplete.4.dart | 14 ++++----- .../search_anchor/search_anchor.4.dart | 7 ++--- packages/flutter_tools/lib/src/dart/pub.dart | 30 ++++++++----------- 4 files changed, 22 insertions(+), 36 deletions(-) diff --git a/examples/api/lib/material/autocomplete/autocomplete.3.dart b/examples/api/lib/material/autocomplete/autocomplete.3.dart index 60ccea126d..8fc690e7a6 100644 --- a/examples/api/lib/material/autocomplete/autocomplete.3.dart +++ b/examples/api/lib/material/autocomplete/autocomplete.3.dart @@ -132,11 +132,8 @@ _Debounceable _debounce(_Debounceable function) { debounceTimer = _DebounceTimer(); try { await debounceTimer!.future; - } catch (error) { - if (error is _CancelException) { - return null; - } - rethrow; + } on _CancelException { + return null; } return function(parameter); }; diff --git a/examples/api/lib/material/autocomplete/autocomplete.4.dart b/examples/api/lib/material/autocomplete/autocomplete.4.dart index a44107b6d2..bd669c6f7f 100644 --- a/examples/api/lib/material/autocomplete/autocomplete.4.dart +++ b/examples/api/lib/material/autocomplete/autocomplete.4.dart @@ -71,14 +71,13 @@ class _AsyncAutocompleteState extends State<_AsyncAutocomplete > { late final Iterable options; try { options = await _FakeAPI.search(_currentQuery!, _networkEnabled); - } catch (error) { - if (error is _NetworkException) { + } on _NetworkException { + if (mounted) { setState(() { _networkError = true; }); - return []; } - rethrow; + return []; } // If another search happened after this one, throw away these options. @@ -189,11 +188,8 @@ _Debounceable _debounce(_Debounceable function) { debounceTimer = _DebounceTimer(); try { await debounceTimer!.future; - } catch (error) { - if (error is _CancelException) { - return null; - } - rethrow; + } on _CancelException { + return null; } return function(parameter); }; diff --git a/examples/api/lib/material/search_anchor/search_anchor.4.dart b/examples/api/lib/material/search_anchor/search_anchor.4.dart index d7e90ea12a..4f14bdbd2d 100644 --- a/examples/api/lib/material/search_anchor/search_anchor.4.dart +++ b/examples/api/lib/material/search_anchor/search_anchor.4.dart @@ -139,11 +139,8 @@ _Debounceable _debounce(_Debounceable function) { debounceTimer = _DebounceTimer(); try { await debounceTimer!.future; - } catch (error) { - if (error is _CancelException) { - return null; - } - rethrow; + } on _CancelException { + return null; } return function(parameter); }; diff --git a/packages/flutter_tools/lib/src/dart/pub.dart b/packages/flutter_tools/lib/src/dart/pub.dart index 36714c2b75..b0f3dfe7b1 100644 --- a/packages/flutter_tools/lib/src/dart/pub.dart +++ b/packages/flutter_tools/lib/src/dart/pub.dart @@ -411,23 +411,19 @@ class _DefaultPub implements Pub { exitCode = result.exitCode; } - // The exception is rethrown, so don't catch only Exceptions. - } catch (exception) { // ignore: avoid_catches_without_on_clauses - if (exception is io.ProcessException) { - final StringBuffer buffer = StringBuffer('${exception.message}\n'); - final String directoryExistsMessage = _fileSystem.directory(directory).existsSync() - ? 'exists' - : 'does not exist'; - buffer.writeln('Working directory: "$directory" ($directoryExistsMessage)'); - buffer.write(_stringifyPubEnv(pubEnvironment)); - throw io.ProcessException( - exception.executable, - exception.arguments, - buffer.toString(), - exception.errorCode, - ); - } - rethrow; + } on io.ProcessException catch (exception) { + final StringBuffer buffer = StringBuffer('${exception.message}\n'); + final String directoryExistsMessage = _fileSystem.directory(directory).existsSync() + ? 'exists' + : 'does not exist'; + buffer.writeln('Working directory: "$directory" ($directoryExistsMessage)'); + buffer.write(_stringifyPubEnv(pubEnvironment)); + throw io.ProcessException( + exception.executable, + exception.arguments, + buffer.toString(), + exception.errorCode, + ); } final int code = exitCode;