0

For the BMI app created in flutter, I am writing a test, so when I run the test I am getting this below error:

═╡ 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

This is my flutter test code:

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

I tried to initialize it like this but got the same error:

 setUpAll(() async{
     TestWidgetsFlutterBinding.ensureInitialized();
     await ScreenUtilInit();
   });

tried googling it but couldn't find how to initialize ScreenUtil() in the flutter test. Any help would be appreciated.

1 Answers1

2

I faced the same issue, all what I did is that, wrap your home_screen with ScreenUtilInit:

 await tester.pumpWidget(
      MaterialApp(
          home: ScreenUtilInit(
        designSize: Size(360, 690),
        allowFontScaling: false,
        child: home_screen(),
      )),
    );
Ayoub Boumzebra
  • 3,885
  • 3
  • 21
  • 37