Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access the final parent StatefulWidget class variables from its extended class?

I’ve ExperienceCardFront a StatefulWidget class which extends _ExperienceCardFrontState.

I’m trying to access the variables (marked as final and required) in ExperienceCardFront from its state, _ExperienceCardFrontState.

This is my ExperienceCardFront:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class ExperienceCardFront extends StatefulWidget {
  const ExperienceCardFront(
      {Key? key, required this.title, required this.image})
      : super(key: key);

  //the 2 variables I want to get access to from the extended class
  final String title;
  final String image;

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

And this is my _ExperienceCardFrontState:

class _ExperienceCardFrontState extends State<ExperienceCardFront> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return Container(
       //here I want to get access to the title (or image) variable from ExperienceCardFront
       child: Text(title),
    );
  }

}

The following error is thrown: Undefined name 'title', when I try to run this code.

>Solution :

**Try this **

class _ExperienceCardFrontState extends State<ExperienceCardFront> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return Container(
       //here I want to get access to the title (or image) variable from ExperienceCardFront
       child: Text(widget.title),
    );
  }

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading