2

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.
}
irongirl
  • 765
  • 3
  • 12
  • 29

2 Answers2

7

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.

Doc
  • 9,076
  • 3
  • 26
  • 52
1

You can use text in _CreateLevelState using widget.text.

Keerti Purswani
  • 4,065
  • 3
  • 12
  • 27