【问题标题】:ERROR in flutter: widget_test.dart cannot detect MyApp()颤振中的错误:widget_test.dart 无法检测到 MyApp()
【发布时间】:2019-10-23 14:43:15
【问题描述】:

作为一个初学者,我正在尝试各种颤振功能,但由于 widget_test.dart 文件中的错误,我一直无法运行 main.dart。 请指出错误是否是由于其他原因造成的。

ma​​in.dart

import 'package:flutter/material.dart';
 void main(){
   var app = MaterialApp(
     title: 'FlutterApp',
     debugShowCheckedModeBanner: true,
     theme: ThemeData(
        primaryColor: Colors.black12,
        accentColor: Colors.orange,
     ),
     home: Scaffold(
       appBar: AppBar(
         title: Text('Stateless'),
         backgroundColor: Colors.black,
         ),
   ),
   );
   runApp(app);
 }

widget_test.dart

// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:stateless/main.dart';

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

    // Verify that our counter starts at 0.
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);

    // Tap the '+' icon and trigger a frame.
    await tester.tap(find.byIcon(Icons.add));
    await tester.pump();

    // Verify that our counter has incremented.
    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
  });
}

这是我的第一个问题,如果我不能以正确的方式提出问题,我非常抱歉

【问题讨论】:

    标签: flutter-test


    【解决方案1】:

    如果您也告诉我们您收到的错误消息会更好。但是,据我所知,widget_test.dart 中没有定义 MyApp。

    您可以在另一个文件中定义 MyApp 小部件,然后将其导入您的 widget_test.dart。

    一个例子是:

    another_file.dart

    class MyApp extends StatelessWidget {
      const MyApp({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
         title: 'FlutterApp',
         debugShowCheckedModeBanner: true,
         theme: ThemeData(
            primaryColor: Colors.black12,
            accentColor: Colors.orange,
         ),
         home: Scaffold(
           appBar: AppBar(
             title: Text('Stateless'),
             backgroundColor: Colors.black,
             ),
       ),);
      }
    }
    

    widget_test.dart

    import 'package:flutter/material.dart';
    import 'package:flutter_test/flutter_test.dart';
    
    import 'package:stateless/another_file.dart';
    
    void main() {
      testWidgets('My test', (WidgetTester tester) async {
        // Build our app and trigger a frame.
        await tester.pumpWidget(MyApp());
      });
    }
    

    【讨论】:

    • 感谢您的回答。它确实奏效了。但是在分析了代码和错误后,我发现错误在这一行:primaryColor:Colors.black12。由于我不知道的某种原因,不支持 Colors.black12。
    • 你导入了 material.dart 吗?
    • 在我的情况下,我使用了相同的解决方案,在我再次将另一个文件重命名为 main 之后,它似乎是一个 IDE 错误
    【解决方案2】:

    很简单,你可以在 widget_test.dart 文件中将 MyApp 名称替换为你的类名。

    例如。在 widget_test.dart 文件中用 app(在你的情况下)替换 MyApp

    【讨论】:

      【解决方案3】:

      创建名为another_file.dart的文件后

      您必须添加:

      import '../test/another_file.dart';
      

      在widget_test.dart

      【讨论】:

        【解决方案4】:

        我刚刚发现,解决方案只是输入您实际的 Stateful Widget 名称,而不是默认 Tap Counter 应用程序提供的默认“MyApp”!

        【讨论】:

          【解决方案5】:

          在 main.dart 文件中你的 App 名称是 MaterialApp,因此在 widget_test.dart 文件中将 MyApp 名称更改为 MaterialApp 就这么简单。

          我也是 Flutter 的新手,但解决这类小问题让我有头脑,而不是给出一些难以理解的解决方案。

          【讨论】:

          • MaterialApp 是flutter框架提供的widget。在测试中创建它只会测试框架而不是应用程序。
          【解决方案6】:

          为此,您只需添加材料应用程序,它就可以正常工作。让我展示我的代码。我也面临类似的问题:

          Main.dart

          void main() {
            runApp(
              MaterialApp(
                home: Scaffold(
                  appBar: AppBar(
                    backgroundColor: Colors.grey[600],
                    title: Text("I Am Rich"),
                  ),
                ),
              ),
            );
          }
          

          widget_test.dart

                // Verify that our counter starts at 0.
                expect(find.text('0'), findsOneWidget);
                expect(find.text('1'), findsNothing);
          
                // Tap the '+' icon and trigger a frame.
                await tester.tap(find.byIcon(Icons.add));
                await tester.pump();
          
                // Verify that our counter has incremented.
                expect(find.text('0'), findsNothing);
                expect(find.text('1'), findsOneWidget);
              },
            );
          }
          

          在这段代码中,我简单地编写了 MaterialApp 来代替 MyApp,它也可以正常工作并且简单易用。

          【讨论】:

            【解决方案7】:

            解决方案

            在您的pubspec.yaml 顶部,确保验证您的项目名称与测试小部件的导入语句中列出的导入包的名称匹配。

            验证此信息后,运行 flutter pub get 以再次同步依赖项。

            在我的情况下,这解决了不正确的名称解析。

            【讨论】:

              【解决方案8】:

              只需删除 widget_test.dart 文件中的所有行。清空吧。。 它用于测试小部件(不是必需的)......

              【讨论】:

              • 删除需要解决的问题究竟是如何解决问题的?请阅读How to Answer!
              • 把它清空,它用于测试(非可选)
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-05-26
              • 1970-01-01
              • 1970-01-01
              • 2021-07-04
              • 2021-12-18
              • 2022-09-30
              • 1970-01-01
              相关资源
              最近更新 更多