children property

List<Widget> children
final

The widgets to display.

If this list is going to be mutated, it is usually wise to put a Key on each of the child widgets, so that the framework can match old configurations to new configurations and maintain the underlying render objects.

Also, a Widget in Flutter is immutable, so directly modifying the children such as someWidget.children.add(...) or passing a reference of the original list value to the children parameter will result in incorrect behaviors. Whenever the children list is modified, a new list object must be provided.

The following code corrects the problem mentioned above.

class SomeWidgetState extends State<SomeWidget> {
  final List<Widget> _children = <Widget>[];

  void someHandler() {
    setState(() {
      // The key here allows Flutter to reuse the underlying render
      // objects even if the children list is recreated.
      _children.add(ChildWidget(key: UniqueKey()));
    });
  }

  @override
  Widget build(BuildContext context) {
    // Always create a new list of children as a Widget is immutable.
    return PageView(children: List<Widget>.of(_children));
  }
}

Implementation

final List<Widget> children;