user14789593
user14789593

Reputation:

How to receive a parameter and use in initState within a stateful widget

I have a stateful widget that has one method called in initialisation. I wanna know how to be able to get a parameter from the previous screen and pass it in initState to my initialisation method

class LabDetalheWidget extends StatefulWidget {
  final String path;

  const LabDetalheWidget({
    Key key,
    this.path,
  }) : super(key: key);

Upvotes: 2

Views: 4119

Answers (2)

KR Tirtho
KR Tirtho

Reputation: 464

I believe you wanna pass data across routes.. If so then read this flutter cookbook's section you might get the idea https://flutter.dev/docs/cookbook/navigation/passing-data

Upvotes: 0

YoBo
YoBo

Reputation: 2529

You can pass parameter like that

class MyWidget extends StatefulWidget {
  final String param;

  const MyWidget({
    Key key,
    this.param,
  }) : super(key: key);
  
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
    void initState() {
      print(widget.param);
      super.initState();
    }
  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}

Inside the state you can access the parameter like that

print(widget.param)

Upvotes: 2

Related Questions