Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lint 'discarded_futures'. #4659

Merged
merged 10 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ linter:
# - constant_identifier_names # https://github.com/dart-lang/linter/issues/204
- control_flow_in_finally
- directives_ordering
- discarded_futures
- empty_catches
- empty_constructor_bodies
- empty_statements
Expand Down
18 changes: 12 additions & 6 deletions case_study/memory_leaks/memory_leak_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';

import 'about.dart';
Expand Down Expand Up @@ -86,15 +88,19 @@ class MyHomeState extends State<MyHome> with SingleTickerProviderStateMixin {
void showMenuSelection(String value) {
switch (value) {
case logMenu:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Logger()),
unawaited(
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Logger()),
),
);
break;
case aboutMenu:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => About()),
unawaited(
Navigator.push(
context,
MaterialPageRoute(builder: (context) => About()),
),
);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ class MyGetHttpDataState extends State<MyGetHttpData> {
super.initState();

// Call the getJSONData() method when the app initializes
getJSONData();
unawaited(getJSONData());
}
}
52 changes: 28 additions & 24 deletions case_study/memory_leaks/memory_leak_app/lib/tabs/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';

import '../common.dart';
Expand Down Expand Up @@ -34,31 +36,33 @@ class LoggerState extends State<Logger> {

// ignore: unused_element
void _pushSaved() {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
final tiles = _saved.map(
(itemValue) {
return ListTile(
title: Text(
itemValue,
style: _biggerFont,
),
);
},
);
final divided = ListTile.divideTiles(
context: context,
tiles: tiles,
).toList();
unawaited(
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
final tiles = _saved.map(
(itemValue) {
return ListTile(
title: Text(
itemValue,
style: _biggerFont,
),
);
},
);
final divided = ListTile.divideTiles(
context: context,
tiles: tiles,
).toList();

return Scaffold(
appBar: AppBar(
title: const Text('Saved lists'),
),
body: ListView(children: divided),
);
},
return Scaffold(
appBar: AppBar(
title: const Text('Saved lists'),
),
body: ListView(children: divided),
);
},
),
),
);
}
Expand Down
10 changes: 7 additions & 3 deletions case_study/memory_leaks/memory_leak_app/lib/tabs/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';

import '../logging.dart';
Expand Down Expand Up @@ -61,9 +63,11 @@ class SettingsState extends State<Settings> {
restfulApi = key;
});
// Display the data received.
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyGetHttpData()),
unawaited(
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyGetHttpData()),
),
);
},
);
Expand Down
4 changes: 3 additions & 1 deletion case_study/platform_channel/lib/channel_demo.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Expand All @@ -14,7 +16,7 @@ class _ChannelDemoState extends State<ChannelDemo> {
late String _response;

void _sendMessage() {
_channel.send('Message from Dart');
unawaited(_channel.send('Message from Dart'));
}

void _reset() {
Expand Down
10 changes: 7 additions & 3 deletions case_study/platform_channel/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';

import 'channel_demo.dart';
Expand Down Expand Up @@ -30,9 +32,11 @@ class _HomePage extends StatelessWidget {
child: TextButton(
style: TextButton.styleFrom(backgroundColor: Colors.green),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChannelDemo()),
unawaited(
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChannelDemo()),
),
);
},
child: const Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AnalyticsController {
}) : _analyticsEnabled = ValueNotifier<bool>(enabled),
_shouldPrompt = ValueNotifier<bool>(firstRun && !enabled) {
if (_shouldPrompt.value) {
toggleAnalyticsEnabled(true);
unawaited(toggleAnalyticsEnabled(true));
}
if (_analyticsEnabled.value) {
setUpAnalytics();
Expand Down
17 changes: 10 additions & 7 deletions packages/devtools_app/lib/src/analytics/prompt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -105,9 +107,11 @@ class _AnalyticsPromptState extends State<AnalyticsPrompt>
textTheme.titleMedium?.copyWith(color: const Color(0xFF54C1EF)),
recognizer: TapGestureRecognizer()
..onTap = () {
launchUrl(
'https://www.google.com/intl/en/policies/privacy',
context,
unawaited(
launchUrl(
'https://www.google.com/intl/en/policies/privacy',
context,
),
);
},
),
Expand All @@ -127,7 +131,7 @@ class _AnalyticsPromptState extends State<AnalyticsPrompt>
ElevatedButton(
onPressed: () {
// This will also hide the prompt.
controller.toggleAnalyticsEnabled(false);
unawaited(controller.toggleAnalyticsEnabled(false));
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.grey),
child: const Text('No thanks.'),
Expand All @@ -137,9 +141,8 @@ class _AnalyticsPromptState extends State<AnalyticsPrompt>
),
ElevatedButton(
onPressed: () {
controller
..toggleAnalyticsEnabled(true)
..hidePrompt();
unawaited(controller.toggleAnalyticsEnabled(true));
controller.hidePrompt();
},
child: const Text('Sounds good!'),
),
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools_app/lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class DevToolsAppState extends State<DevToolsApp> with AutoDisposeMixin {
void initState() {
super.initState();

ga.setupDimensions();
unawaited(ga.setupDimensions());

addAutoDisposeListener(serviceManager.isolateManager.mainIsolate, () {
setState(() {
Expand Down
26 changes: 18 additions & 8 deletions packages/devtools_app/lib/src/charts/flame_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,22 +392,30 @@ abstract class FlameChartState<T extends FlameChart,
// TODO(kenz): zoom in/out faster if key is held. It actually zooms slower
// if the key is held currently.
if (keyLabel == 'w') {
zoomTo(
math.min(
maxZoomLevel,
currentZoom + keyboardZoomInUnit,
unawaited(
zoomTo(
math.min(
maxZoomLevel,
currentZoom + keyboardZoomInUnit,
),
),
);
} else if (keyLabel == 's') {
zoomTo(
math.max(
FlameChart.minZoomLevel,
currentZoom - keyboardZoomOutUnit,
unawaited(
zoomTo(
math.max(
FlameChart.minZoomLevel,
currentZoom - keyboardZoomOutUnit,
),
),
);
} else if (keyLabel == 'a') {
// `unawaited` does not work for FutureOr
// ignore: discarded_futures
scrollToX(horizontalControllerGroup.offset - keyboardScrollUnit);
} else if (keyLabel == 'd') {
// `unawaited` does not work for FutureOr
// ignore: discarded_futures
scrollToX(horizontalControllerGroup.offset + keyboardScrollUnit);
}
}
Expand Down Expand Up @@ -478,6 +486,8 @@ abstract class FlameChartState<T extends FlameChart,
// to call this that guarantees the scroll controller offsets will be
// updated for the new zoom level and layout size
// https://github.com/flutter/devtools/issues/2012.
// `unawaited` does not work for FutureOr
// ignore: discarded_futures
scrollToX(newScrollOffset, jump: true);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class DragAndDropManagerWeb extends DragAndDropManager {

@override
void dispose() {
onDragOverSubscription.cancel();
onDragLeaveSubscription.cancel();
onDropSubscription.cancel();
unawaited(onDragOverSubscription.cancel());
unawaited(onDragLeaveSubscription.cancel());
unawaited(onDropSubscription.cancel());
super.dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _ExampleConditionalScreenBodyState
final json =
offlineController.offlineDataJson[ExampleConditionalScreen.id];
if (json.isNotEmpty) {
loadOfflineData(json['title']);
unawaited(loadOfflineData(json['title']));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools_app/lib/src/framework/initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class _InitializerState extends State<Initializer>
!connectionState.userInitiatedConnectionState) {
// Try to reconnect (otherwise, will fall back to showing the
// disconnected overlay).
_attemptUrlConnection();
unawaited(_attemptUrlConnection());
}
});

Expand All @@ -88,7 +88,7 @@ class _InitializerState extends State<Initializer>
serviceManager.onConnectionAvailable.listen((_) => setState(() {})),
);

_attemptUrlConnection();
unawaited(_attemptUrlConnection());
}

@override
Expand All @@ -97,7 +97,7 @@ class _InitializerState extends State<Initializer>

// Handle widget rebuild when the URL has changed.
if (widget.url != null && widget.url != oldWidget.url) {
_attemptUrlConnection();
unawaited(_attemptUrlConnection());
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/devtools_app/lib/src/framework/landing_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:devtools_shared/devtools_shared.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -158,7 +160,8 @@ class _ConnectDialogState extends State<ConnectDialog>
SizedBox(
width: scaleByFontFactor(350.0),
child: TextField(
onSubmitted: actionInProgress ? null : (str) => _connect(),
onSubmitted:
actionInProgress ? null : (str) => unawaited(_connect()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
actionInProgress ? null : (str) => unawaited(_connect()),
actionInProgress ? null : (str) async => _connect(),

autofocus: true,
decoration: const InputDecoration(
isDense: true,
Expand Down Expand Up @@ -265,7 +268,7 @@ class ImportFileInstructions extends StatelessWidget {
),
const SizedBox(height: defaultSpacing),
ElevatedButton(
onPressed: () => _importFile(context),
onPressed: () => unawaited(_importFile(context)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onPressed: () => unawaited(_importFile(context)),
onPressed: () async => _importFile(context),

child: const MaterialIconLabel(
label: 'Import File',
iconData: Icons.file_upload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ class ReleaseNotes extends AnimatedWidget {
: Expanded(
child: Markdown(
data: markdownData!,
onTapLink: (_, href, __) => launchUrl(href!, context),
onTapLink: (_, href, __) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onTapLink: (_, href, __) =>
onTapLink: (_, href, __) async =>
launchUrl(href!, context),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like the fixes to go to separate PRs. They may affect functionality and tests. And I do not want this massive PR to be held by this and thus become victim of error-prone merges.

Created bug: #4668

unawaited(launchUrl(href!, context)),
),
),
],
Expand Down
3 changes: 2 additions & 1 deletion packages/devtools_app/lib/src/http/http_request_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

Expand Down Expand Up @@ -44,7 +45,7 @@ class DartIOHttpRequestData extends NetworkRequest {
}) : wrapperId = _dartIoHttpRequestWrapperId++,
super(timelineMicrosBase) {
if (requestFullDataFromVmService && _request.isResponseComplete) {
getFullRequestData();
unawaited(getFullRequestData());
}
}

Expand Down
Loading