Skip to content

Add discarded_futures lint #1572

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

Closed
wants to merge 11 commits into from
Closed
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
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 'package:flutter/services.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -44,7 +46,7 @@ void showCell() {
class CounterModel extends ChangeNotifier {
CounterModel() {
_channel.setMethodCallHandler(_handleMessage);
_channel.invokeMethod<void>('requestCounter');
unawaited(_channel.invokeMethod<void>('requestCounter'));
}

final _channel = const MethodChannel('dev.flutter.example/counter');
Expand All @@ -54,7 +56,7 @@ class CounterModel extends ChangeNotifier {
int get count => _count;

void increment() {
_channel.invokeMethod<void>('incrementCounter');
unawaited(_channel.invokeMethod<void>('incrementCounter'));
}

Future<dynamic> _handleMessage(MethodCall call) async {
Expand Down Expand Up @@ -175,7 +177,7 @@ class Contents extends StatelessWidget {
if (showExit) ...[
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => SystemNavigator.pop(),
onPressed: () async => SystemNavigator.pop(),
child: const Text('Exit this screen'),
),
],
Expand Down
8 changes: 4 additions & 4 deletions add_to_app/books/flutter_module_books/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class _BookDetailState extends State<BookDetail> {
icon: const Icon(Icons.clear),
// Pressing clear cancels the edit and leaves the activity without
// modification.
onPressed: () {
hostApi.cancel();
onPressed: () async {
await hostApi.cancel();
clear();
},
),
Expand All @@ -125,8 +125,8 @@ class _BookDetailState extends State<BookDetail> {
icon: const Icon(Icons.check),
// Pressing save sends the updated book to the platform.
onPressed: book != null
? () {
hostApi.finishEditingBook(book!);
? () async {
await hostApi.finishEditingBook(book!);
clear();
}
: null,
Expand Down
8 changes: 5 additions & 3 deletions add_to_app/fullscreen/flutter_module/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 'package:flutter/services.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -32,7 +34,7 @@ void main() {
class CounterModel extends ChangeNotifier {
CounterModel() {
_channel.setMethodCallHandler(_handleMessage);
_channel.invokeMethod<void>('requestCounter');
unawaited(_channel.invokeMethod<void>('requestCounter'));
}

final _channel = const MethodChannel('dev.flutter.example/counter');
Expand All @@ -42,7 +44,7 @@ class CounterModel extends ChangeNotifier {
int get count => _count;

void increment() {
_channel.invokeMethod<void>('incrementCounter');
unawaited(_channel.invokeMethod<void>('incrementCounter'));
}

Future<dynamic> _handleMessage(MethodCall call) async {
Expand Down Expand Up @@ -151,7 +153,7 @@ class Contents extends StatelessWidget {
if (showExit) ...[
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => SystemNavigator.pop(animated: true),
onPressed: () async => SystemNavigator.pop(animated: true),
child: const Text('Exit this screen'),
),
],
Expand Down
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 'package:flutter/services.dart';
import 'package:url_launcher/url_launcher.dart' as launcher;
Expand Down Expand Up @@ -61,7 +63,7 @@ class _MyHomePageState extends State<MyHomePage> {

void _incrementCounter() {
// Mutations to the data model are forwarded to the host platform.
_channel.invokeMethod<void>("incrementCount", _counter);
unawaited(_channel.invokeMethod<void>("incrementCount", _counter));
}

@override
Expand All @@ -87,7 +89,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
TextButton(
onPressed: () {
_channel.invokeMethod<void>("next", _counter);
unawaited(_channel.invokeMethod<void>("next", _counter));
},
child: const Text('Next'),
),
Expand Down
8 changes: 5 additions & 3 deletions add_to_app/plugin/flutter_module_using_plugin/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 'package:flutter/services.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -43,7 +45,7 @@ void showCell() {
class CounterModel extends ChangeNotifier {
CounterModel() {
_channel.setMethodCallHandler(_handleMessage);
_channel.invokeMethod<void>('requestCounter');
unawaited(_channel.invokeMethod<void>('requestCounter'));
}

final _channel = const MethodChannel('dev.flutter.example/counter');
Expand All @@ -53,7 +55,7 @@ class CounterModel extends ChangeNotifier {
int get count => _count;

void increment() {
_channel.invokeMethod<void>('incrementCounter');
unawaited(_channel.invokeMethod<void>('incrementCounter'));
}

Future<dynamic> _handleMessage(MethodCall call) async {
Expand Down Expand Up @@ -174,7 +176,7 @@ class Contents extends StatelessWidget {
if (showExit) ...[
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => SystemNavigator.pop(),
onPressed: () async => SystemNavigator.pop(),
child: const Text('Exit this screen'),
),
],
Expand Down
8 changes: 5 additions & 3 deletions add_to_app/prebuilt_module/flutter_module/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 'package:flutter/services.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -32,7 +34,7 @@ void main() {
class CounterModel extends ChangeNotifier {
CounterModel() {
_channel.setMethodCallHandler(_handleMessage);
_channel.invokeMethod<void>('requestCounter');
unawaited(_channel.invokeMethod<void>('requestCounter'));
}

final _channel = const MethodChannel('dev.flutter.example/counter');
Expand All @@ -42,7 +44,7 @@ class CounterModel extends ChangeNotifier {
int get count => _count;

void increment() {
_channel.invokeMethod<void>('incrementCounter');
unawaited(_channel.invokeMethod<void>('incrementCounter'));
}

Future<dynamic> _handleMessage(MethodCall call) async {
Expand Down Expand Up @@ -151,7 +153,7 @@ class Contents extends StatelessWidget {
if (showExit) ...[
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => SystemNavigator.pop(animated: true),
onPressed: () async => SystemNavigator.pop(animated: true),
child: const Text('Exit this screen'),
),
],
Expand Down
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linter:
cancel_subscriptions: true
close_sinks: true
directives_ordering: true
discarded_futures: true
package_api_docs: true
package_prefixed_library_names: true
test_types_in_equals: true
Expand Down
5 changes: 3 additions & 2 deletions animations/lib/main.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:io';

import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -45,13 +46,13 @@ void setupWindow() {
setWindowTitle('Animation Samples');
setWindowMinSize(const Size(windowWidth, windowHeight));
setWindowMaxSize(const Size(windowWidth, windowHeight));
getCurrentScreen().then((screen) {
unawaited(getCurrentScreen().then((screen) {
setWindowFrame(Rect.fromCenter(
center: screen!.frame.center,
width: windowWidth,
height: windowHeight,
));
});
}));
}
}

Expand Down
5 changes: 2 additions & 3 deletions animations/lib/src/basics/02_page_route_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class PageRouteBuilderDemo extends StatelessWidget {
body: Center(
child: ElevatedButton(
child: const Text('Go!'),
onPressed: () {
Navigator.of(context).push<void>(_createRoute());
},
onPressed: () async =>
Navigator.of(context).push<void>(_createRoute()),
),
),
);
Expand Down
6 changes: 3 additions & 3 deletions animations/lib/src/basics/06_custom_tween.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ class _CustomTweenDemoState extends State<CustomTweenDemo>
actions: [
MaterialButton(
textColor: Colors.white,
onPressed: () {
onPressed: () async {
if (controller.status == AnimationStatus.completed) {
controller.reverse().whenComplete(() {
await controller.reverse().whenComplete(() {
setState(() {});
});
} else {
controller.forward().whenComplete(() {
await controller.forward().whenComplete(() {
setState(() {});
});
}
Expand Down
8 changes: 4 additions & 4 deletions animations/lib/src/basics/08_fade_transition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ class _FadeTransitionDemoState extends State<FadeTransitionDemo>
),
ElevatedButton(
child: const Text('animate'),
onPressed: () => setState(() {
_controller.animateTo(1.0).then<TickerFuture>(
(value) => _controller.animateBack(0.0));
}),
onPressed: () async {
await _controller.animateTo(1.0);
await _controller.animateBack(0.0);
},
),
],
),
Expand Down
8 changes: 4 additions & 4 deletions animations/lib/src/misc/card_swipe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ class _SwipeableCardState extends State<SwipeableCard>

/// Runs the fling / spring animation using the final velocity of the drag
/// gesture.
void _dragEnd(DragEndDetails details) {
Future<void> _dragEnd(DragEndDetails details) async {
final size = context.size;

if (size == null) {
return;
}

var velocity = (details.velocity.pixelsPerSecond.dx / size.width).abs();
_animate(velocity: velocity);
await _animate(velocity: velocity);
}

void _updateAnimation(double dragPosition) {
Expand All @@ -185,12 +185,12 @@ class _SwipeableCardState extends State<SwipeableCard>
));
}

void _animate({double velocity = 0}) {
Future<void> _animate({double velocity = 0}) async {
var description =
const SpringDescription(mass: 50, stiffness: 1, damping: 1);
var simulation =
SpringSimulation(description, _controller.value, 1, velocity);
_controller.animateWith(simulation).then<void>((_) {
await _controller.animateWith(simulation).then<void>((_) {
widget.onSwiped();
});
}
Expand Down
4 changes: 2 additions & 2 deletions animations/lib/src/misc/focus_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class SmallCard extends StatelessWidget {
return Card(
child: Material(
child: InkWell(
onTap: () {
onTap: () async {
var nav = Navigator.of(context);
nav.push<void>(_createRoute(context, imageAssetName));
await nav.push<void>(_createRoute(context, imageAssetName));
},
child: Image.asset(
imageAssetName,
Expand Down
2 changes: 1 addition & 1 deletion animations/lib/src/misc/hero_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class HeroAnimationDemo extends StatelessWidget {
color: Colors.grey.shade300,
),
),
onTap: () => Navigator.of(context).push<void>(
onTap: () async => Navigator.of(context).push<void>(
MaterialPageRoute(builder: (context) => const HeroPage())),
),
);
Expand Down
Loading