Search code examples
fluttersetstatedart-pubstatefulwidget

Flutter extended_image: setState() or markNeedsBuild() called during build


I am using the package extended_image to load images from the network and display a shimmer on loading or on error.

I am getting this error setState() or markNeedsBuild() called during build when I am trying to call setState inside the loadStateChanged

In fact, I have two widgets, one VideoThumbnail responsible for loading a thumbnail from the network, and another one VideoDesc that should display the thumbnail description.

But I would like the description to display a shimmer when the image fails to load or is taking longer to load.

I created two states variables, on the VideoThumbnail widget, that should be passed to the VideoDesc widget

videoLoading = true;
videoError = false;

Here is my code following the repo example:

VideoThumbnail State

class _VideoThumbnailState extends State<VideoThumbnail>
    with SingleTickerProviderStateMixin {
  bool videoLoading;
  bool videoError;

  AnimationController _controller;

  @override
  void initState() {
    videoLoading = true;
    videoError = false;

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    );
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      print("Build Process Complete");
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.circular(4.0),
            child: ExtendedImage.network(
              widget.videoUrl,
              width: widget.width,
              height: (widget.width) * 3 / 4,
              loadStateChanged: (ExtendedImageState state) {
                switch (state.extendedImageLoadState) {
                  case LoadState.loading:
                    _controller.reset();

                    setState(() {
                      videoError = false;
                      videoLoading = true;
                    });

                    return Shimmer.fromColors(
                      child: Container(
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(4.0),
                        ),
                      ),
                      baseColor: Colors.black12,
                      highlightColor: Colors.white24,
                    );
                    break;

                  case LoadState.completed:
                    _controller.forward();

                    setState(() {
                      videoError = false;
                      videoLoading = false;
                    });

                    return FadeTransition(
                      opacity: _controller,
                      child: ExtendedRawImage(
                        image: state.extendedImageInfo?.image,
                        width: widget.width,
                        height: (widget.width) * 3 / 4,
                      ),
                    );

                    break;

                  case LoadState.failed:
                    _controller.reset();
                    state.imageProvider.evict();

                    setState(() {
                      videoError = true;
                      videoLoading = false;
                    });

                    return Container(
                      width: widget.width,
                      height: (widget.width) * 3 / 4,
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage("assets/img/not-found.png"),
                          fit: BoxFit.fill,
                        ),
                      ),
                    );

                    break;

                  default:
                    return Container();
                }
              },
            ),
          ),
          VideoDesc(
            desc: widget.desc,
            videoError: videoError,
            videoLoading: videoLoading,
          )
        ],
      ),
    );
  }
}

Video widget

class VideoDesc extends StatelessWidget {
  final String desc;
  final bool videoLoading;
  final bool videoError;

  const VideoDesc({
    Key key,
    @required this.desc,
    this.videoLoading = true,
    this.videoError = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: videoError || videoLoading
          ? Shimmer.fromColors(
              baseColor: Colors.grey[700],
              highlightColor: Colors.white24,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(height: 12.0),
                  Container(
                    width: double.infinity,
                    height: 8.0,
                    decoration: BoxDecoration(
                      color: Colors.grey[900],
                      borderRadius: BorderRadius.circular(2.0),
                    ),
                  ),
                  SizedBox(height: 12.0),
                  Container(
                    width: 80.0,
                    height: 8.0,
                    decoration: BoxDecoration(
                      color: Colors.grey[900],
                      borderRadius: BorderRadius.circular(2.0),
                    ),
                  ),
                ],
              ),
            )
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                SizedBox(height: 12.0),
                Text(
                  desc,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 11.0,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
                SizedBox(height: 5.0),
                Text(
                  "361,143,203 views",
                  style: TextStyle(
                    color: Colors.white54,
                    fontSize: 12.0,
                  ),
                ),
              ],
            ),
    );
  }
}

Can anyone help me with this problem? Or if there is a better way to get the extendedImageLoadState value and pass it to another widget without calling the setState inside loadStateChanged


Solution

  • You can't call setState during build process.

    If you actually need to, you can do so by using instead:

    WidgetsBinding.instance.addPostFrameCallback(() => setState((){}));
    

    However, have in mind, that having this on your switch-case will schedule an infinite loop of rebuilds which you don't want as well.

    I suggest you to re-structure your UI logic or at least make it conditional:

    if(!videoLoading) {
     WidgetsBinding.instance.addPostFrameCallback(() => setState((){
            videoError = false;
            videoLoading = true;  
    }));
    }