Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Data can be delivered to a StatefulWidget in Flutter by passing it through the widget constructor or by using the InheritedWidget/InheritedModel approach.

To retrieve the data within the StatefulWidget's state, the widget's state must be accessed through the createState() method, which returns a mutable state object (_MyWidgetState in the example below). Then, the data can be accessed and manipulated within the state object's methods.

Example:

class MyWidget extends StatefulWidget {
  final String data;

  MyWidget({Key key, this.data}) : super(key: key);

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

class _MyWidgetState extends State<MyWidget> {
  String _data;

  @override
  void initState() {
    super.initState();
    _data = widget.data;
  }

  @override
  Widget build(BuildContext context) {
    // Access _data and build widget
  }
}