对于在颤振中创建的 BMI 应用程序,我正在编写一个测试,所以当我运行测试时,我收到以下错误:
═╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═
The following assertion was thrown building home_screen(dirty, state: _home_screenState#60944):
Ensure to initialize ScreenUtil before accessing it.
Please execute the init method : ScreenUtil.init()
'package:flutter_screenutil/screen_util.dart':
Failed assertion: line 32 pos 7: '_instance != null'
The relevant error-causing widget was:
home_screen file:///C:/Users/bmi/test/test.dart:14:32
When the exception was thrown, this was the stack:
#2 new ScreenUtil (package:flutter_screenutil/screen_util.dart:32:7)
#3 _home_screenState.build (package:bmi/ui_screen/HomeScreen.dart:54:22)
#4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)
#6 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4667:11)
#7 Element.rebuild (package:flutter/src/widgets/framework.dart:4189:5)
#8 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4474:5)
#9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4658:11)
#10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4469:5)
#11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14)
#12 Element.updateChild (package:flutter/src/widgets/framework.dart:3303:20)
#13 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1182:16)
#14 RenderObjectToWidgetElement.update (package:flutter/src/widgets/binding.dart:1160:5)
#15 RenderObjectToWidgetElement.performRebuild (package:flutter/src/widgets/binding.dart:1174:7)
#16 Element.rebuild (package:flutter/src/widgets/framework.dart:4189:5)
#17 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2694:33)
#18 AutomatedTestWidgetsFlutterBinding.drawFrame (package:flutter_test/src/binding.dart:1102:19)
#19 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:319:5)
#20 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#21 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#22 AutomatedTestWidgetsFlutterBinding.pump.<anonymous closure> (package:flutter_test/src/binding.dart:969:9)
#25 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:72:41)
#26 AutomatedTestWidgetsFlutterBinding.pump (package:flutter_test/src/binding.dart:956:27)
#27 WidgetTester.pumpWidget.<anonymous closure> (package:flutter_test/src/widget_tester.dart:522:22)
#30 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:72:41)
#31 WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:519:27)
#32 main.<anonymous closure> (file:///C:/Users/bmi/test/test.dart:14:21)
#33 main.<anonymous closure> (file:///C:/Users/bmi/test/test.dart:12:32)
#34 testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:144:29)
<asynchronous suspension>
<asynchronous suspension>
(elided 7 frames from class _AssertionError, dart:async, and package:stack_trace)
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞══
The following TestFailure object was thrown running a test:
Expected: exactly 2 matching nodes in the widget tree
Actual: _WidgetTypeFinder:<zero widgets with type "GestureDetector" (ignoring offstage widgets)>
Which: means none were found but some were expected
When the exception was thrown, this was the stack:
#4 main.<anonymous closure> (file:///C:/Users/bmi/test/test.dart:17:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
...
This was caught by the test expectation on the following line:
file:///C:/Users/bmi/test/test.dart line 17
The test description was:
age increment
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞══
The following message was thrown:
Multiple exceptions (2) were detected during the running of the current test, and at least one was
unexpected.
Test failed. See exception logs above.
The test description was: age increment
这是我的颤振测试代码:
import 'package:bmi_app_one/ui_screen/HomeScreen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main(){
TestWidgetsFlutterBinding.ensureInitialized();
testWidgets('age increment', (WidgetTester ageTester) async {
await ageTester.pumpWidget(home_screen());
// To verify that age counter starts at 20
expect(find.byType(GestureDetector), findsNWidgets(2));
expect(find.text('0'), findsNothing);
});
}
我试图像这样初始化它,但得到了同样的错误:
setUpAll(() async{
TestWidgetsFlutterBinding.ensureInitialized();
await ScreenUtilInit();
});
尝试用谷歌搜索它,但在颤振测试中找不到如何初始化 ScreenUtil() 。任何帮助,将不胜感激。