Skip to content

Nullable future generated as return type for async functions that return a nullable value #54865

Open
@adwiv

Description

Issue description

The return type generated by quickfix in vscode, studio or dartpad for the following async function is Future<String?>?. In my opinion, it should be Future<String?> instead, as an async function would always return a Future and never null.

returnStringOrNull() async 
{
  return Random().nextBool() ? "OK" : null;
}

Dart SDK version: 3.2.6 (stable) (Wed Jan 24 13:41:58 2024 +0000) on "macos_x64"

Steps to replicate

Here is a dartpad showing the same issue - https://dartpad.dev/?id=0e8505321de7157abe72675f4e1ab073

Right click on returnStringOrNull and then select Add return type. The resulting code is

Future<String?>? returnStringOrNull() async 
{
  return Random().nextBool() ? "OK" : null;
}

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onanalyzer-quick-fixarea-analyzerUse area-analyzer for Dart analyzer issues, including the analysis server and code completion.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions