How to pass data to a Stateful Widget?

Pass your data when you Navigate screen :

Navigator.of(context).push(MaterialPageRoute(builder: (context) => DetailScreen(data:"data")));

Access the data using constructor with widget  keyword

class DetailScreen extends StatefulWidget {
  final String? data;
  const DetailScreen({Key? key, this.data}) : super(key: key);

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

class _DetailScreenState extends State {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(
        "${widget.data}",
        style: const TextStyle(fontSize: 18.0),
      ),
    );
  }
}

How to pass data to a Stateful Widget?

How to pass data to a Stateful Widget?

 

Leave a Comment