'The method 'setState' isn't defined for the class MyApp error in Flutter
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;
});
},
),
),
),
)
);
}
Solution 1:[1]
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
}
Solution 2:[2]
place
setState
inside
StatefullWidget
:) that will solve the problem
Solution 3:[3]
You have to call that function within a stateful widget
Solution 4:[4]
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 option + return (or cmd + . if you use VS Code on macOS),
Solution 5:[5]
Be Sure you typed right
setState( () {} ) ;
Solution 6:[6]
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).
Solution 7:[7]
Use a stateful widget to do so
Whenever you change the internal state of a State object, make the change in a function that you pass to setState:
setState(() { _myState = newValue; });
Resolve to the above question, like this:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
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;
});
},
),
),
),
)
);
}
}
Solution 8:[8]
Keep the cursor above the StatelessWidget and press Alt + insert and click on convert to StatefulWidget, to quickly covert your StatelessWidget to StatefulWidget.
Solution 9:[9]
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++;
Solution 10:[10]
setState{} is only available inside a Stateful Widget class/subclass. You need to convert your Stateless Widget to a StatefulWidget.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow

