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

FlutterFlutter Test

Flutter Problem Overview


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,

Flutter Solutions


Solution 1 - Flutter

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.

Solution 2 - Flutter

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

Solution 3 - Flutter

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.

Solution 4 - Flutter

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.

Solution 5 - Flutter

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

Solution 6 - Flutter

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.

Solution 7 - Flutter

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeonardoView Question on Stackoverflow
Solution 1 - Flutterhysabone.comView Answer on Stackoverflow
Solution 2 - FlutterMrMalithView Answer on Stackoverflow
Solution 3 - FlutterFawzy ZayedView Answer on Stackoverflow
Solution 4 - FlutterPadula PankajaView Answer on Stackoverflow
Solution 5 - FlutterLil WebheadView Answer on Stackoverflow
Solution 6 - FlutterRansford OwusuView Answer on Stackoverflow
Solution 7 - FlutterlightlessdaysView Answer on Stackoverflow