颤振中的错误:widget_test.dart无法检测到MyApp()

时间:2019-06-08 10:07:08

标签: flutter-test

作为一个完整的初学者,我正在尝试各种flutter功能,由于widget_test.dart文件中的错误,我被困在运行main.dart上。 请指出错误是由于其他原因造成的。

main.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);
  });
}

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

7 个答案:

答案 0 :(得分:2)

创建名为 another_file.dart 的文件后

您必须添加:

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

widget_test.dart

答案 1 :(得分:0)

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

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

一个例子是:

another_file.dart

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "venmz.settings")

widget_test.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,
         ),
   ),);
  }
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

很简单,您只需在widget_test.dart文件中将MyApp名称替换为您的类名称即可。

eg. replace MyApp with app(in your case) in widget_test.dart file

答案 4 :(得分:0)

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

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,它也能正常工作,而且简单易行。

答案 5 :(得分:-1)

您的应用程序名称在main.dart文件中为MaterialApp,因此在widget_test.dart文件中将MyApp名称更改为MaterialApp就这么简单。

我也是新手,但是解决这类小问题会让我有所准备,而不是提供一些令人困惑的解决方案。

答案 6 :(得分:-1)

只需删除 widget_test.dart 文件中的所有行。清空吧。。 用于测试widgets(非必须)......