0

I want to change a bool value when the image is loaded.

The code is the following:

           child: Image.network(
                                _db.image.path,
                                loadingBuilder: (BuildContext context,
                                    Widget child,
                                    ImageChunkEvent loadingProgress) {
                                  if (loadingProgress == null) {
                                    widget.isFetched = true;
                                    print('fetched');
                                    return Container(
                                      width: 200,
                                      child: child,
                                    );
                                  }
                                  return Container(
                                    width: 200,
                                    height: 200,
                                    child: Center(
                                      child: CircularProgressIndicator(
                                        value: loadingProgress
                                                    .expectedTotalBytes !=
                                                null
                                            ? loadingProgress
                                                    .cumulativeBytesLoaded /
                                                loadingProgress
                                                    .expectedTotalBytes
                                            : null,
                                      ),
                                    ),
                                  );
                                },
                              ),
       

Somehow the widget.isFeched=true gets triggered right after the screen appears and then again when the image is loaded.

What is the solution to avoid it and only change the value to true when the image is loaded?

Ballazx
  • 431
  • 4
  • 12

1 Answers1

0

your code is fine, you just need to add setstate() like this :

// setState will update the ui instantly
                    if (loadingProgress == null) {
                      setState(() {
                        widget.isFetched = true;
                        print('fetched');
                        return Container(
                          width: 200,
                          child: child,
                        );
                      });
                    }
Lucky Trail
  • 161
  • 8