Why setState not re-render my child stateful widget

1,783 views
Skip to first unread message

Bảo Nguyễn Ngọc

unread,
Mar 6, 2021, 1:33:23 AM3/6/21
to Flutter Development (flutter-dev)
This is my code:

var _showGoogleMaps = false;

@override
  void initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 2000), () => setState(() {
    _showGoogleMaps = true;
    } ));
  }

@override
  Widget build(BuildContext context) {
    return 
      //example 1
      // _showGoogleMaps ? Container(height: 500, color: Colors.red)
      //          : Container(height: 500, color: Colors.blue);

      //example 2
      FullImageView(
      imgCodeVal: imgCodeVal,
      child: Container(height: 500, color: _showGoogleMaps ? Colors.blue : Colors.red));

      //example 3
      // Container(
      // child: Container(height: 500, color: _showGoogleMaps ? Colors.blue : Colors.red));
  }



In example 1 and 3, the container color will change from red to blue after 2 seconds.

But in example 2, the color is red and not changed. FullImageView is just a normal stateful widget

I dont understand why. Anyone knows the reason. Thanks

Suzuki Tomohiro

unread,
Mar 6, 2021, 7:58:44 AM3/6/21
to Bảo Nguyễn Ngọc, Flutter Development (flutter-dev)
This article explains the problem that you can solve with Keys.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/d0ea63b0-9f5c-4f92-bc81-423a4ab2810an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages