Flutter - setState not updating inner Stateful Widget

后端 未结 5 470
生来不讨喜
生来不讨喜 2021-02-01 02:57

Basically I am trying to make an app whose content will be updated with an async function that takes information from a website, but when I do try to set the new state, it doesn

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 03:07

    This was really giving me headache and no Google results were working. What finally worked was so simple. In your child build() assign the value to the local variable before you return. Once I did this everything worked with subsequent data loads. I even took out the initState() code.

    Many thanks to @Simon. Your answer somehow inspired me to try this.

    In your childState:

    @override
    Widget build(BuildContext context) {
    _title = widget._title; // <<< ADDING THIS HERE IS THE FIX
    return new GestureDetector(onTap: foo(),
       child: new Card(child: new Text(_title));
    
    }
    

    Hopefully this works in your code. For me, I use a Map for the entire JSON record passed in, rather than a single String, but that should still work.

提交回复
热议问题