Search code examples
flutterdartflutter-layoutflutter-testdart-null-safety

The name 'MyApp' isn't a class. Try correcting the name to match an existing class. flutter


I don't know what is the problem is / how to fix this while I tring to code it shows this error on the test folder widget_test.dart

enter image description herex

 testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(const MyApp());

Solution

  • On the pumpWidget method change "const MyApp()" to the same name of your first Widget class. The class you instance on the runApp function, inside the main function on the main.dart file.

    For example, if your main.dart content is:

    void main() async {
         runApp(MyGreatApp());
    }
            
    class MyGreatApp extends StatelessWidget {
       const MyGreatApp();
            
      @override
      Widget build(BuildContext context) {
           return MaterialApp(
                  //rest of code ...
           );
      }
    }
    

    Put in your widget_test.dart file:

    await tester.pumpWidget(const MyGreatApp()); //<-