Closed
Description
Describe the issue
I believe that the discarded_futures
lint is too aggressive. Read on.
To Reproduce
Let's see the following simple Flutter code:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyWidget(),
);
}
}
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
Future<String> createFuture(String someParam) async {
await Future<void>.delayed(const Duration(seconds: 1));
return Future.value('dash');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<String>(
future: createFuture('hi'),
builder: (context, snapshot) {
return const Text('just a placeholder');
},
),
);
}
}
And let's enable discarded_futures
lint in analysis_options.yaml
:
include: package:flutter_lints/flutter.yaml
linter:
rules:
discarded_futures: true
And let's run dart analyze
:
$ dart analyze
Analyzing discarded_futures_lint_bug... 1.0s
info • lib/main.dart:35:17 • Don't invoke asynchronous functions in non-async blocks. •
discarded_futures
1 issue found.
That's how it looks like in my VSCode:
Expected behavior
I'd expect no warnings. FutureBuilder.future
expects a future, I'm giving it the future, we're happy, but there's discarded_futures
yelling at us.
Additional context
$ flutter --version
Flutter 3.3.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision e3c29ec00c (3 weeks ago) • 2022-09-14 08:46:55 -0500
Engine • revision a4ff2c53d8
Tools • Dart 2.18.1 • DevTools 2.15.0
Also:
This might be another issue, but I often see code like this:
BlocProvider<AuthCubit>(
lazy: false,
create: (_) => AuthCubit(
loginClient: context.read(),
forIntegrationtest: _forIntegrationTest,
)..init(), // init is async
),
The discarded_futures
lint also warns about that ..init()
, but we never care about it. Implementing the lint that is talked about in this issue would solve this problem.
Metadata
Metadata
Assignees
Labels
A lower priority bug or feature requestFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.Issues with the analyzer's support for the linter packageIssues related to lint rules that report a problem when it isn't a problem.Incorrect behavior (everything from a crash to more subtle misbehavior)