Skip to content

[proposal] Force user to at least use one or both from initalData or Future in FutureBuilder #83081

Closed

Description

@agwanyaseen

Use case

While learning flutter FutureBuilder , I was just creating a small POC for my understanding i.e fetching data from Rest Api (randomuser.me). In FutureBuilder default constructor we have three things from which initalData and future is optional.

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

While making POC I forgot to supply both(future and initialData) but everything seems working fine. Untill I found it's not fetching data from web Api. I had spend around 15 minutes checking why my code is not fetching data from REST Api. Then I saw future is missing.

Flutter kept executing builder without even woorying about future which seems to bad UX for me as a developer. We should atleast warn or force developers to provide a future or initialData.

Eg.

FutureBuilder<String>(															 
          builder:(ctx,snapshot)=>CircularProgressIndicator(),
        );

Everthing will work fine on above code, but will not work as expected since we haven't supplied necessary arguments.

Proposal

I think we should add one more validation(assert) in FutureBuilder<String>(). Which will force developers to supply atleast one or both from future and initialData.

Below is what I think can be done to improve UX of developing apps in flutter.

const  FutureBuilder({
  Key? key,
  this.future,
  this.initialData,
  required  this.builder,
}) : assert(builder != null),
assert(!(future == null && initialData == null),'Future and initialData cannot be null at same time'),
super(key: key);

I have already started working on these. Just want to know if these can be accepted or we should move in any other direction.


These is my First Proposal or Feature Request. Pardon me for any mistakes.

Activity

  1. added
    in triagePresently being triaged by the triage team
    frameworkflutter/packages/flutter repository. See also f: labels.
    c: proposalA detailed proposal for a change to Flutter
    and removed
    in triagePresently being triaged by the triage team
    on May 21, 2021
  2. agwanyaseen commented on May 25, 2021

    @agwanyaseen
    Author

    what's next on these?

  3. kenthinson commented on Jun 29, 2021

    @kenthinson

    I would caution that this might break a lot of apps. In my app I start without the Future defined (null), then at runtime depending on the users choice it is created.

    I would need to provide dummy data in initalData

    or use gate logic to not display the FutureBuilder until after the choice is made.
    If I'm using logic to block display of the FutureBuilder then I would need to wrap the FutureBuilder in an additional Builder.
    Not the end of the world, but still less convenient.

    Being able to put all related build logic into one build function is convenient.

    First we gate:
    if(hasData)
    return data display

    if(hasError)
    return error message

    if(connectionState == waiting)
    return loading spinner

    etc.

    return Default Layout with user choices.

  4. added
    P3Issues that are less important to the Flutter project
    on Apr 20, 2022
  5. caseycrogers commented on Apr 20, 2023

    @caseycrogers
    Contributor

    Carrying over from my duplicate issue:
    imo the important part is that the developer be required to specify a future. They can still manually set it to null, they just can't omit it:

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

    While there are perfectly good reasons to pass a nullable future (see @kenthinson's earlier comment) I can't think of a coherent case where a developer would intentionally omit the future argument. And if there is a coherent case, they can simply write future: null.
    Same applies for StreamBuilder.stream. Both should be changed at once to keep the two APIs consistent with eachother.

  6. domesticmouse commented on Apr 21, 2023

    @domesticmouse
    Contributor

    I'd vote that this is worth promoting higher than P5 as it makes learning flutter with futures less error prone.

    I'm assuming this is a breaking API change and should be initially built out as a lint with a fix before introducing a breaking API change?

  7. changed the title [-][proposal] Force user to atleast use one or both from initalData or Future in FutureBuilder[/-] [+][proposal] Force user to at least use one or both from initalData or Future in FutureBuilder[/+] on Apr 21, 2023
  8. goderbauer commented on May 2, 2023

    @goderbauer
    Member

    Making them required sounds reasonable.

    I'm assuming this is a breaking API change

    This depends on whether the change makes any customer tests fail... if it doesn't we can just make the change.

  9. github-actions commented on May 16, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions