Another exception was thrown: type 'MyApp' is not a subtype of type 'StatelessWidget'

Leonardo picture Leonardo · Jul 14, 2018 · Viewed 17.4k times · Source

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,

Answer

musausman.com picture musausman.com · Nov 7, 2018

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.