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 pass variables into a stateful widget?

I’m trying to learn how to pass variables into a stateful widget.

here is my code:

class TestTest extends StatefulWidget {
  final String selectedText;
  const TestTest({Key? key, required this.selectedText,})
      : super(key: key);

  @override
  State<TestTest> createState() => _TestTestState();
}

class _TestTestState extends State<TestTest> {
  String optionAData = '';
  String optionBData = '';
  @override
  Widget build(BuildContext context) {
    switch (selectedText) {
      case 'OptionA':
        return Column(
          children: [
            WidgetDisplay1(
              onTap: (){
                setState(() {
                  optionAData = 'A123';
                });
              },
            ),
            WidgetDisplay3(
              onTap: (){
                setState(() {
                  optionAData = 'A456';
                });
              },
            ),
          ],
        );
      case 'OptionB':
        return Column(
          children: [
            WidgetDisplay5(
              onTap: (){
                setState(() {
                  optionBData = 'B123';
                });
              },
            ),
            WidgetDisplay3(onTap: (){
              setState(() {
                optionBData = 'B456';
              });
            }),
            WidgetDisplay4(
              onTap: (){
                setState(() {
                  optionBData = 'B789';
                });
              },
            ),
          ],
        );
      default:
        return Container();
    }
  }
}

this is how i’ve been told to do it but the selectedText beside the Switch key word is returning the error: Undefined name ‘selectedText’. which I thought I had defined at the top of the class with the final string selectedText.

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

Thanks so much and any help would be greatly appreciated.

>Solution :

So, to access the data when receiving from widget, you must place the keyword "widget" before the name of the variable. With that in mind, your code will be something like this:

class TestTest extends StatefulWidget {
final String selectedText;
const TestTest({Key? key, required this.selectedText,})
  : super(key: key);

  @override
  State<TestTest> createState() => _TestTestState();
}

class _TestTestState extends State<TestTest> {
String optionAData = '';
String optionBData = '';
@override
Widget build(BuildContext context) {
  switch (widget.selectedText) { //! Here's the main difference
    case 'OptionA':
      return Column(
        children: [
          WidgetDisplay1(
            onTap: (){
              setState(() {
                optionAData = 'A123';
              });
            },
          ),
          WidgetDisplay3(
            onTap: (){
              setState(() {
                optionAData = 'A456';
              });
            },
          ),
        ],
      );
    case 'OptionB':
      return Column(
        children: [
          WidgetDisplay5(
            onTap: (){
              setState(() {
                optionBData = 'B123';
              });
            },
          ),
          WidgetDisplay3(onTap: (){
            setState(() {
              optionBData = 'B456';
            });
          }),
          WidgetDisplay4(
            onTap: (){
              setState(() {
                optionBData = 'B789';
              });
            },
          ),
        ],
      );
    default:
      return Container();
  }
}

Hope it helped!

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