0

How to manage to force a Widget to rebuild immediately after one of the property value has changed? Some pseudo code:

class Live extends StatefulWidget {

  String name;
Live(this.name);

  @override
  _LiveState createState() => _LiveState();
}

class _LiveState extends State<Live> {

 // some turbo logic I don't want to move to Live class
 ..

  @override
  Widget build(BuildContext context) {
    return Column(childrens: [
    Text(widget.name),
    Card(content calculated based on turbo logic),
    ]
      
    );
  }
}

When String name property has updated (based on parent's setState call), everything is happening in real time. The change is reflected immediately in Text widget. The value is visible immediately only because i am using widget.name call so in built() method I am using property from Live class instead of State.

The problem is that another widget wrapped in Card is calculated in place marked as // some turbo logic I don't want to move to Live class. Due to this fact when I want to see updates in this section I need to switch tab and go to e.g Setting and then return to Live tab to see changes related to Card content. I believe it trigger build() method again.

Golas:

  • Once the name value is updated in Live widget, a State widget rebuilds immediately.
  • do not move turbo logic to Live class and keep it in State class
Ensei Tankado
  • 270
  • 2
  • 12

1 Answers1

0

First off, your StatefulWidget should be immutable and therefore only contain immutable fields. I'd suggest you move the name field into State and change it there using a setter. The setter should call setState(), this will cause the desired rebuild.

See this introduction for more information - specifically the "Bird" sample code to see how to code a setter.

rgisi
  • 858
  • 6
  • 21
  • I use setState() method in Parent Widget and I believe this method will update child descendants. It's not happening. The example on the flutter side is not showing how to update internal State object from the Outside. – Ensei Tankado Mar 15 '21 at 19:09