Skip to content

Make FutureBuilder.future Required #125188

@caseycrogers

Description

@caseycrogers

Use case

  1. I create a future builder
  2. I write the contents of builder
  3. I forget to provider a future
  4. I run my app and the future builder never receives data
  5. I waste time debugging only to realize the simple mistake

In a toy example, step 5 is very fast. But if I've made a ton of changes at once in many places (eg a large refactor) step 5 can easily take several minutes. And if I'm using FutureBuilder heavily, this coding error can happen fairly often.

Proposal

Make future a required constructor argument.
In the (non-existent???) case where a developer intentionally didn't pass a variable (null or otherwise) for future, they can instead just write future: null.
In the typical case where a developer did intend to provide a variable, they will now get a compiler warning if they forget (and/or they'll never forget in the first place because their IDE auto-generates required arguments).

  const FutureBuilder({
    super.key,
    this.initialData,
    required this.future,
    required this.builder,
  }) : assert(builder != null);

Also, by the exact same logic, StreamBuilder.stream should be a required argument too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: duplicateIssue is closed as a duplicate of an existing issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions