This question already has answers here:
Passing data to StatefulWidget and accessing it in it's state in Flutter
(7 answers)
Closed 3 years ago.
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.
}
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.
You can use text in _CreateLevelState using widget.text.
Related
createState:
Creates the mutable state for this widget at a given location in the tree.
https://api.flutter.dev/flutter/widgets/StatefulWidget/createState.html
Now, in code:
class A extends StatefulWidget {
#override
_AState createState() => _AState();
}
class _AState extends State<A> {
}
Here we create a separate class named _AState which inherits from a predefined class named State?
So, what is createState' role here? How does it create a mutable state for us?
With createState you tell the StatefulWidget which class to use as state for this widget. And you tell it here that it needs to create an instance of _AState for this widget.
By the way, it is also recommended to write it as
State<A> createState() => _AState();
It still works the way you wrote it but the IDE might complain about
Avoid using private types in public APIs
Saying the return type is State<A> instead of _AState removes this warning.
In order to create a stateful widget in a flutter, it uses the createState() method. Stateful Widget means a widget that has a mutable state.
I want to have a StatefulWidget where I can pass the initial value for a non-nullable member of the widgets State from the widgets constructor.
My current solution (see below) seems to be not ideal, I see two problems with it:
The initial value has to be saved in the widget itself before passing it to the state.
The member in the sate has to be marked as late since it can only be set after initialization.
Is there a better way to initialize a StatefulWidget's state non-nullable member from a value passed to the widget constructor?
My current implementation:
class MyWidget extends StatefulWidget {
final String text;
const MyWidget({Key? key, required this.text}) : super(key: key);
#override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
late String text;
#override
void initState() {
text = widget.text;
super.initState();
}
#override
Widget build(BuildContext context) {
return Text(text);
}
}
(Not shown here, but later the text member should be changeable, that's why it is in the State)
hey there your code seems good.
but the better way is using bloc to pass and receive data.
any way . its not necessary to pass and fill data in initstate and _MyWidgetState .
you can receive your data directly in build widget As you wrote (widget.text)
here is some good things for avoid nullable
https://codewithandrea.com/videos/dart-null-safety-ultimate-guide-non-nullable-types/
You could use the constructor of State like this: _MyWidgetState(){ text=widget.text; }. The constructor will certainly be executed before initState and build methods.
I am trying to apply this state management but i cannot implement it. If I use stateless widget, i can easily implement it but stateful it is complicated and i cannot achieve it. Where to implement cubits? Here is my code part =>
class MaintenanceScreen extends StatefulWidget {
int? locId;
MaintenanceScreen({required this.locId, Key? key}) : super(key: key);
#override
_MaintenanceScreenState createState() => _MaintenanceScreenState();
}
class _MaintenanceScreenState extends State<MaintenanceScreen> {
final colFr = FrenchColors();
if you mean you want to access locId in _MaintenanceScreenState
simply you can use widget.locId inside the build function and flutter will bind this with the widget class.
This question already has answers here:
Passing data to StatefulWidget and accessing it in it's state in Flutter
(7 answers)
Closed 1 year ago.
I have this Stateful class and I wanna took "Data" from Stateful class to State class
Here is my code:
class PlayMode extends StatefulWidget {
final Data data;
PlayMode({this.data}); // Pass this to State class
#override
State<StatefulWidget> createState() {
return new PlayModeState();
}
}
How it can be done?
In your PlayModeState class, simply use
widget.data
This question already has answers here:
Dart Multiple Constructors
(10 answers)
How can I create multiple constructors in dart?
(3 answers)
Closed 2 years ago.
class HomePage extends StatefulWidget {
final String uid;
HomePage({Key key, #required this.uid}) : super(key: key);
final FirebaseUser user;
HomePage({this.user});
#override
_HomePageState createState() => _HomePageState(uid);
}
The default constructor is already defined.
Try giving one of the constructors a name.dart(duplicate_constructor)
i want this two construters to pass on any one can help me in his
You're getting the error because you're trying to make two default constructor. Try making the second one a named constructor to fix the issue.
Note : Dart doesn't support's constructor and method overloading. That's why it comes with named methods that makes them more readable and easy to manage.
class HomePage extends StatefulWidget {
final String uid;
HomePage({Key key, #required this.uid}) : super(key: key);
final FirebaseUser user;
HomePage.user({this.user});
#override
_HomePageState createState() => _HomePageState(uid);
}