Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variable from stateful widget class? - flutter [duplicate]

Tags:

state

flutter

I have a string that contains an information that was passed from previous classes. But i need to use that variable in the state class.

Class of stateful widget that contains the information (String text):

class CreateLevelScreen extends StatefulWidget {
  String text;
  CreateLevelScreen({Key key, @required this.text}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _CreateLevelState();
}

State class of stateful widget to retrieve that information text too.

class _CreateLevelState extends State<CreateLevelScreen> {

   //need to pass text in here to use it too.
}
like image 477
irongirl Avatar asked Dec 22 '25 21:12

irongirl


1 Answers

When you say

class _CreateLevelState extends State<CreateLevelScreen>

it means _CreateLevelState will manage the state of CreateLevelScreen

so the variables are directly accessible as widget.<variable_name>

thus you have widget.textto be used in _CreateLevelState class if there is a variable text in your CreateLevelScreen class.

like image 162
Doc Avatar answered Dec 24 '25 09:12

Doc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!