Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 291 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The method 'setState' isn't defined for the class MyApp error in Flutter

#1
I am getting the error `The method 'setState' isn't defined for the class MyApp error in Flutter` in the `onSubmitted` of the `TextField`

Code:

Widget build(BuildContext context) {

String phoneNo;

return new MaterialApp(
title: 'SchoolTrack',
theme: new ThemeData(
primaryColor: Colors.grey[50],
),
home: new Scaffold(
appBar: null,

backgroundColor: Colors.cyan[100],

body: new Container(

padding: const EdgeInsets.all(32.0),
child: new Center(
child: new TextField(
autofocus: true,
autocorrect: false,


decoration: new InputDecoration(
hintText: 'Type the phone no',
suffixIcon: new Icon(Icons.send),
suffixStyle: new TextStyle(
color: Colors.cyan[300],
)
),

onSubmitted: (String input) {
setState(() {
phoneNo = input;
});
},

),
),
),
)
);
}
Reply

#2
You have to call that function within a stateful widget
Reply

#3
I assume you are trying to setState in a stateless widget, which is immutable(unable to change).

Use a stateful widget to do so, like this:

class MainPage extends StatefulWidget{
HomePage createState()=> HomePage();
}

class HomePage extends State<MainPage>{
//Your code here
}
Reply

#4
place

> setState

inside

> StatefullWidget

:) that will solve the problem
Reply

#5
Keep the cursor above the ``StatelessWidget`` and press ``Alt + insert`` and click on convert to ``StatefulWidget``, to quickly covert your ``StatelessWidget`` to ``StatefulWidget``.
Reply

#6
`setState` is only available inside a `StatefulWidget` class/subclass. You need to convert your `StatelessWidget` to a `StatefulWidget`. Simply:

Click on the `StatelessWidget` class and use <kbd>option</kbd> + <kbd>return</kbd> (or <kbd>cmd</kbd> + <kbd>.</kbd> if you use VS Code on macOS),


[![enter image description here][1]][1]

[1]:
Reply

#7
Be Sure you typed right

setState( () {} ) ;
Reply

#8
If you are using a lambda and getting 'setstate isn't referenced' be sure to type it right with the brackets:
```
setState(() => _counter++);
```

instead of:
```
setState() => _counter++;
```
Reply

#9
As mentioned above, the issue stem from StatelessWidget. For people who use text editors for coding the easiest way is like following:
Replace this:

class YourClassName extends StatelessWidget {

With this:

class YourClassName extends StatefulWidget {
@override
_YourClassNameState createState() => _YourClassNameState();
}

class _YourClassNameState extends State<YourClassName> {

All other things will be the same and now you can call setState inside your class:

setState(() {
whatever you want to update goes here
});

However, it is more practical to put your setState inside your own function and trigger it from anywhere easily:

void funtionName(passing parameters) {
setState(() {
....
});
}

Now call your function easily from anywhere like: funtionName(parameter).



Reply

#10
setState{} is only available inside a Stateful Widget class/subclass. You need to convert your Stateless Widget to a StatefulWidget.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through