Reputation: 733
I have just started using Flutter and i'm having this problem while running my code "Another exception was thrown: type 'MyApp' is not a subtype of type 'StatelessWidget'". And the interesting part is that i dont even have this 'StatelessWidget' in my code.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
List<String> _bars = ['Olivio bar'];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Drinkzz'),
),
body: Column(
children: [
Container(
margin: EdgeInsets.all(10.0),
child: RaisedButton(
onPressed: () {
_bars.add('Riviera Bar');
},
child: Text('Add new Bar!'),
),
),
Column(
children: _bars
.map((element) => Card(
child: Column(
children: <Widget>[
Image.asset('assets/olivio.jpg'),
Text(element)
],
),
))
.toList(),
)
],
)),
);
}
}
I am really lost and would aprecciate some help!
Thanks,
Upvotes: 71
Views: 36669
Reputation: 564
Quick Fix for Windows: To fix this, you need to stop main.dart by pressing Ctrl+F2 and run it again by pressing Shift+F10.
Basically, you will need to restart your app.
Upvotes: 3
Reputation: 114
There is a simple way to solve this problem
Create a class above the "MyApp" class and call it any name you like, for instance "RealApp" but which is stateless and put "MyApp" stateful widget into it like so:
class RealApp extends StatelessWidget{
Widget build(Buildcontexr context){
@override
return MyApp();
}
The point is that you cannot run a Stateful widget in the runApp function but to get around this problem put the stateful widget into a stateless widget and your problem is solved.
Upvotes: 0
Reputation: 11
Since StatelessWidget was used at the begining of the code and was latter changed to a StatefulWidget you need to hot restart. It will continue to give error if you don't restart it because initially it's invoked in main
Upvotes: 1
Reputation: 104
In the flutter If you need to change MyApp then you can not get result when after the app reloaded .You must need to restart you app and then you can check your edits are available on the app.
Upvotes: 5
Reputation: 136
You need to Hot Reload by using R (shift + r) coz you change MyApp class from StatelessWidget to a StatefulWidget while your app is running.
Upvotes: 12
Reputation: 1372
you need to restart your app for the changes to take effect. Hot reload won't work at this time
Upvotes: 18
Reputation: 3055
If you changed
MyApp
from aStatelessWidget
to aStatefulWidget
you need to hot restart, since it is invoked in main
This has been explained multiple times in live coding sessions, that when you make changes in functions like initState()
, you have to restart the app. A similar case applies for you, when you changed state related properties of the MyApp widget you need to restart your app for those changes to take effect.
Basically, when you hot reload the app, it calls the build()
function, initState()
is called only when you restart the app, so that the app reinitiates everything including the widget whose initState()
function you changed.
Upvotes: 181