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

joe*_*oel 7 flutter-test

作为一个初学者,我正在尝试各种颤振功能,但由于 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);
 }
Run Code Online (Sandbox Code Playgroud)

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);
  });
}
Run Code Online (Sandbox Code Playgroud)

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

wen*_*OaR 10

如果您也告诉我们您收到的错误消息会更好。但是,据我所知,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,
         ),
   ),);
  }
}
Run Code Online (Sandbox Code Playgroud)

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());
  });
}
Run Code Online (Sandbox Code Playgroud)