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
  • 895
  • 7
  • 14
  • 31

2 Answers2

8

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
  • 10,831
  • 3
  • 39
  • 63
1

You can use text in _CreateLevelState using widget.text.

Keerti Purswani
  • 4,878
  • 3
  • 16
  • 29