Skip to main content

pass-existing-future-to-future-builder

Warns when a newly created future is passed to the FutureBuilder.

The future must have been obtained earlier, e.g. during State.initState, State.didUpdateWidget, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.

Example

❌ Bad:

class SomeWidget extends Widget {

Widget build(BuildContext context) {
// LINT: Avoid passing a newly created future to FutureBuilder.
// Try moving the future to a variable and using it instead.
return FutureBuilder(
future: getValue(),
builder: ...,
);
}

Future<String> getValue() => Future.value('str');
}

✅ Good:

class SomeWidget extends Widget {
final _local = getValue();


Widget build(BuildContext context) {
return FutureBuilder(
future: _local,
builder: ...,
);
}

Future<String> getValue() => Future.value('str');
}

Additional Resources