Search code examples
flutterdartvariableswidgetglobal-variables

How to use created variables in every class?


I have a Widget where the user can input data like height, weight and so on in a TextFormField like this:

String weightText;


TextFormField(
                      initialValue: null,
                      keyboardType: TextInputType.number,
                      onChanged: (value) => weightText = value,
                    ),

Now I want to use this variable weightText in other classes. Is there another way except constructor to make the variable weightText global, so I can use it wherever i want?


Solution

  • You must declare the variable as static in a static class. This way you can access it like this Class.variable

    You can create a class like this :

    class Global{
        static String weightText;
    }
    

    and then access the variable like this Global.weightText