HoiPolloi
HoiPolloi

Reputation: 379

Future running twice because it's in build method, how to fix it?

Say I have a function that makes some big file

Future<File> makeBigFile() async {
    // lots of processing
    return File("generated_file.txt");
}

@override
Widget build(BuildContext context) {
  return FutureBuilder(
    future: makeBigFile(),
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      if (snapshot.hasData && snapshot.data is File) {
        return Text("Success!");
      } else if (snapshot.connectionState==ConnectionState.done) {
        return Text("Error!");
      } else {
        return CircularProgressIndicator();
      }
    }
  );
}

so whenever build runs, the future also runs which obviously it shouldn't. The docs say

The future must have been obtained earlier, e.g. during State.initState, State.didUpdateConfig, 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.

From what I understand (which isn't much, despite reading and re-reading the docs) FutureBuilder must be in build() and it needs to have future: that can run multiple times without problems but what if it's some long operation that shouldn't run multiple times?

How should I change my code so it does what it does now but without running the future multiple times?

Upvotes: 3

Views: 776

Answers (2)

Sharad Paghadal
Sharad Paghadal

Reputation: 2154

File myFile;
bool isLoading = false;

Future<File> makeBigFile() async {
    isLoading = true;
    // lots of processing with await methods
    myFile = File("generated_file.txt");
    setState({
        isLoading = false;
    });
}

@override
void initState() {
    makeBigFile();
};

@override
Widget build(BuildContext context) {
    return isLoading 
        ? CircularProgressIndicator()
        : Text( myFile == null ? "Error" : "Success");
}

Upvotes: 1

Filip P.
Filip P.

Reputation: 1354

class BigFileWidget extends StatefulWidget {
  @override
  _BigFileWidgetState createState() => _BigFileWidgetState();
}

class _BigFileWidgetState extends State<BigFileWidget> {

  Future<File> fileFuture;

  @override
  void initState() {
    fileFuture = makeBigFile();
  };

  Future<File> makeBigFile() async {
    // lots of processing
    return File("generated_file.txt");
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: fileFuture,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData && snapshot.data is File) {
            return Text("Success!");
          } else if (snapshot.connectionState==ConnectionState.done) {
            return Text("Error!");
          } else {
            return CircularProgressIndicator();
          }
        }
    );
  }
}

Upvotes: 4

Related Questions