I want to edit my fields in a method like that :
class Test extends StatefulWidget{
Test({this.firstField, this.secondField});
final String firstField ;
final String secondField ;
#override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
#override
Widget build(BuildContext context) {
return null;
}
editField( field, newValue){
//edit any field of the class in a single function here
}
}
If I use widget.firstField as first parameter of my method I will be able to edit it locally ( in the method ) but not the field.
Related
Like the below code:
class TestWidget extends StatefulWidget {
String name;
TestWidget(this.name);
void updateName(String name) {
//how to update Text data
}
#override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<KMSelectionItem> {
#override
Widget build(BuildContext context) {
return Text(widget.name);
}
}
I want to change the text data through the method updateName. But I don't know how can do it.
you should use setState method for this purpose, this method updates the ui based on the changes made. insert the function code like this and declare the function inside your stateful widget. to update the text:
class _TestWidgetState extends State<KMSelectionItem> {
#override
Widget build(BuildContext context) {
return Text(widget.name);
}
void updateName() {
//how to update Text data
setState(() {
widget.name = YOUR_NEW_TEXT;
});
}
}
class TestWidget extends StatefulWidget {
String name;
TestWidget(this.name);
void updateName(String name) {
setState(() {
name ="updated_name";
});
}
#override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<KMSelectionItem> {
#override
Widget build(BuildContext context) {
return Text(widget.name);
}
}
I stumped into a problem where I need to use a StatefulWidget parameter in its state class when it's constructed, but I couldn't find a way to do it since using widget.[whatever variable name] in the state's class constructor returns an unexpected null value, and the initState function only runs when the widget is being drawn to the screen.
For example:
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
final String text;
Test(this.text);
final state = _TestState();
#override
_TestState createState() => state;
}
class _TestState extends State<Test> {
String? changingText;
void updateChangingText(String moreText){
changingText = changingText! + moreText;
}
#override
void initState() {
super.initState();
changingText = widget.text;
}
#override
Widget build(BuildContext context) {
return Text(changingText!);
}
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
var w = Test('test');
w.state.updateChangingText(' text');
return MaterialApp(home: Scaffold(body:
Test('test text')
));
}
}
void main() {
runApp(App());
}
This doesn't work since changingText is being updated before initState gives it its initial value since it only runs when Text is being drawn to the screen and this:
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
final String text;
Test(this.text);
final state = _TestState();
#override
_TestState createState() => state;
}
class _TestState extends State<Test> {
String? changingText;
void updateChangingText(String moreText){
changingText = changingText! + moreText;
}
_TestState(){
changingText = widget.text;
}
#override
Widget build(BuildContext context) {
return Text(changingText!);
}
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
var w = Test('test');
w.state.updateChangingText(' text');
return MaterialApp(home: Scaffold(body:
Test('test text')
));
}
}
void main() {
runApp(App());
}
doesn't work either since you can't use widget.[whatever] in state class constructors (for some reason).
So what can I do to use widget parameters in the state class before the widget is drawn to the screen?
Thanks in advance for the help
You should use the initState method present in the State for this instead of the constructor
#override
void initState() {
changingText = widget.text;
super.initState();
}
In my project, I pass data from one widget to another using this code:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
TranslatorSignUpStep2(transModel: this._translatorModel),
),
);
Then I retrive it in the other widget by the code below:
class TranslatorSignUpStep2 extends StatefulWidget {
final TranslatorModel transModel;
TranslatorSignUpStep2({this.transModel});
#override
_TranslatorSignUpStep2State createState() => _TranslatorSignUpStep2State();
}
The problem is, in the TranslatorSignUpStep2 class I want to assign the passed-in transModel to another variable so I write this code
class _TranslatorSignUpStep2State extends State<TranslatorSignUpStep2> {
TranslatorModel _translatorModel = widget.transModel;
}
But it seems like the widget can't be used outside the build method so I get error saying
The instance member 'widget' can't be accessed in an initializer.. Anyone know how to get over this ?
you can access widget in initState function like this.
class _TranslatorSignUpStep2State extends State<TranslatorSignUpStep2> {
TranslatorModel _translatorModel ;
#override
void initState() {
_translatorModel = widget.transModel;
}
}
Just add the keyword late while initializing:
class TranslatorSignUpStep2 extends StatefulWidget {
final TranslatorModel transModel;
TranslatorSignUpStep2({this.transModel});
#override
_TranslatorSignUpStep2State createState() => _TranslatorSignUpStep2State();
}
class _TranslatorSignUpStep2State extends State<TranslatorSignUpStep2> {
late TranslatorModel _translatorModel = widget.transModel;
}
try this code :
TranslatorSignUpStep2 :
class TranslatorSignUpStep2 extends StatefulWidget {
final TranslatorModel transModel;
TranslatorSignUpStep2({this.transModel});
#override
_TranslatorSignUpStep2State createState() => _TranslatorSignUpStep2State(this.transModel);
}
TranslatorSignUpStep2 class :
class _TranslatorSignUpStep2State extends State<TranslatorSignUpStep2> {
_TranslatorSignUpStep2State(TranslatorModel _tempModel ){
this._translatorModel =_tempModel;
};
TranslatorModel _translatorModel ;
}
Add static keyword when innitializing
class OrderDetails extends StatefulWidget {
int selectedDays;
OrderDetails({
this.range,)};
#override
_OrderDetailsState createState() => _OrderDetailsState();
}
class _OrderDetailsState extends State<OrderDetails> {
String getRange;
#override
void initState() {
super.initState();
getRange = widget.range;
}
I am passing an String from another StatefulWidget to the below CardItem StatefulWidget and then from here the String is passed to the _CardItemState as shown in the code below.
But how this String passed should be accessed from _CardItemState(String itemTitle);
class CardItem extends StatefulWidget {
String itemTitle;
CardItem({#required this.itemTitle});
#override
_CardItemState createState() => _CardItemState(itemTitle);
}
class _CardItemState extends State<CardItem> {
_CardItemState(String itemTitle);
void initState(){
print("INiatialize");
print("itemTitle is " + );
}
}
You can access it via widget.itemTitle. For your case you can call it like this:
printItem(){
print("itemTitle is ${widget.itemTitle}");
}
void initState(){
printItem();
super.initState();
}
You also don't need to pass the string in your state constructor. Remove this:
_CardItemState(String itemTitle);
easy! grab it using
widget.itemTitle
class CardItem extends StatefulWidget {
String itemTitle;
CardItem({#required this.itemTitle});
#override
_CardItemState createState() => _CardItemState(itemTitle);
}
class _CardItemState extends State<CardItem> {
String itemTitle; // - 1
_CardItemState(this.itemTitle); // - 2
void initState(){
itemTitle = widget.itemTitle; // - 3;
print("INiatialize");
print("itemTitle is " + itemTitle ); // - 4
print("itemTitle is " + widget.itemTitle ); // same as - 4
}
}
Best way to do this is to use
widget.fieldName
class CardItem extends StatefulWidget {
String itemTitle;
CardItem({#required this.itemTitle});
#override
_CardItemState createState() => _CardItemState(itemTitle:itemTitle);
}
class _CardItemState extends State<CardItem> {
String myItemTitle;
#override
void initState(){
super.initState();
myItemTitle=widget.itemTitle;
print("INiatialize");
print("itemTitle is $myItemTitle";
}
}
otherway round to this problem is to pass data in constructor as shown below, but the above method is recommended to be followed.
class CardItem extends StatefulWidget {
String itemTitle;
CardItem({#required this.itemTitle});
#override
_CardItemState createState() => _CardItemState(itemTitle:itemTitle);
}
class _CardItemState extends State<CardItem> {
String itemTitle;
_CardItemState({this.itemTitle});
void initState(){
print("INiatialize");
print("itemTitle is $itemTitle";
}
}
I want to access the variable UniversityId in the class of _HomePageState
class showDetailOfUniversity extends StatelessWidget {
final String UniversityId;
showDetailOfUniversity({Key key, #required this.UniversityId}) : super(key:
key);
#override
Widget build(BuildContext context)
{
return (
HomePage()
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
//
var temp2 = showDetailOfUniversity();
var temp = temp2.getUniversityId;
/// here i want to access the code but failed
}
The problem here is that you created another instance of showDetailOfUniversity that will have another values for its members. You did not initialize String UniversityId so its value is null untill you set it.
So when you called showDetailOfUniversity() in temp, the value of String UniversityId in this instance is null since there was no value given in this particular instance.
You can pass the String UniversityId in the constructor of the StatefulWidget like this:
class ShowDetailOfUniversity extends StatelessWidget {
final String universityId;
ShowDetailOfUniversity({Key key, #required this.universityId})
: super(key: key);
#override
Widget build(BuildContext context) {
return HomePage(universityId: universityId);
}
}
class HomePage extends StatefulWidget {
final String universityId;
const HomePage({Key key, this.universityId}) : super(key: key);
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var universityId;
#override
void initState() {
super.initState();
universityId = widget.universityId;
}
#override
Widget build(BuildContext context) {
// TODO: implement build
return Text(universityId);
}
}
Solution for your code:
var temp2 = showDetailOfUniversity(UniversityId: university_id);
var temp = temp2.UniversityId; //this will return university_id