71

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,

4
  • 67
    If you changed MyApp from a StatelessWidget to a StatefulWidget you need to hot restart, since it is invoked in main Jul 14, 2018 at 2:54
  • The issue happends when i press the button!!
    – Leonardo
    Jul 14, 2018 at 2:54
  • I was missing the setState() function!! Thanks
    – Leonardo
    Jul 14, 2018 at 2:58
  • 2
    @Jonah Williams, this is the correct answer, so please post it as an answer and not as a comment
    – Dimitar
    Oct 29, 2018 at 12:59

7 Answers 7

178

As Jonah Williams said,

If you changed MyApp from a StatelessWidget to a StatefulWidget 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.

0
18

you need to restart your app for the changes to take effect. Hot reload won't work at this time

12

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.

5

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.

0
3

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.

1

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.