LateInitializationError:字段“_instance@99075166”尚未初始化
Posted
技术标签:
【中文标题】LateInitializationError:字段“_instance@99075166”尚未初始化【英文标题】:LateInitializationError: Field '_instance@99075166' has not been initialized 【发布时间】:2021-08-28 04:41:52 【问题描述】:这是我第一次测试 Flutter 应用。我试图用这个测试代码来抽我的HomeView
:
void main()
group('Home Test', ()
_pumpHome(WidgetTester tester) => tester.pumpWidget(
MaterialApp(
home: HomeView(),
),
);
testWidgets('Route to Azkar page', (WidgetTester tester) async
await _pumpHome(tester);
await tester.tap(find.byKey(Key('morning')));
expect(find.byType(ListView), findsOneWidget);
);
);
但这两个例外发生了。
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following LateError was thrown building HomeView(dirty):
LateInitializationError: Field '_instance@99075166' has not been initialized.
The relevant error-causing widget was:
HomeView file:///E:/projects/flutterProject/tafra/test/home_test.dart:26:19
When the exception was thrown, this was the stack:
#0 ScreenUtil._instance (package:flutter_screenutil/screen_util.dart)
#1 new ScreenUtil (package:flutter_screenutil/screen_util.dart:28:12)
#2 SizeExtension.w (package:flutter_screenutil/size_extension.dart:5:19)
#3 HomeView.build (package:tafra/app/modules/home/views/home_view.dart:23:27)
#4 StatelessElement.build (package:flutter/src/widgets/framework.dart:4648:28)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#6 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#7 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4553:5)
#8 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5)
... Normal element mounting (166 frames)
#174 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#175 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#176 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
... Normal element mounting (354 frames)
#530 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#531 Element.updateChild (package:flutter/src/widgets/framework.dart:3360:20)
#532 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1189:16)
#533 RenderObjectToWidgetElement.update (package:flutter/src/widgets/binding.dart:1167:5)
#534 RenderObjectToWidgetElement.performRebuild (package:flutter/src/widgets/binding.dart:1181:7)
#535 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#536 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2582:33)
#537 AutomatedTestWidgetsFlutterBinding.drawFrame (package:flutter_test/src/binding.dart:1106:19)
#538 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5)
#539 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#540 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#541 AutomatedTestWidgetsFlutterBinding.pump.<anonymous closure> (package:flutter_test/src/binding.dart:973:9)
#544 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#545 AutomatedTestWidgetsFlutterBinding.pump (package:flutter_test/src/binding.dart:960:27)
#546 WidgetTester.pumpWidget.<anonymous closure> (package:flutter_test/src/widget_tester.dart:534:22)
#549 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:71:41)
#550 WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:531:27)
#551 main.<anonymous closure>._pumpHome (file:///E:/projects/flutterProject/tafra/test/home_test.dart:19:46)
#552 main.<anonymous closure>.<anonymous closure> (file:///E:/projects/flutterProject/tafra/test/home_test.dart:31:22)
#553 main.<anonymous closure>.<anonymous closure> (file:///E:/projects/flutterProject/tafra/test/home_test.dart:30:40)
#554 testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:156:29)
<asynchronous suspension>
<asynchronous suspension>
(elided 5 frames from dart:async and package:stack_trace)
═══════════════════════════════════════════════════════════════════════════════════════════════════╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
The finder "zero widgets with key [<'morning'>] (ignoring offstage widgets)" (used in a call to
"tap()") could not find any matching widgets.
When the exception was thrown, this was the stack:
#0 WidgetController._getElementPoint (package:flutter_test/src/controller.dart:897:7)
#1 WidgetController.getCenter (package:flutter_test/src/controller.dart:836:12)
#2 WidgetController.tap (package:flutter_test/src/controller.dart:271:18)
#3 main.<anonymous closure>.<anonymous closure> (file:///E:/projects/flutterProject/tafra/test/home_test.dart:32:20)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
我认为第二个异常的原因是第一个。
我的HomeView
生产代码:
class HomeView extends GetView<HomeController>
@override
Widget build(BuildContext context)
return Scaffold(
drawer: Drawer(
key: Key('drawer'),
child: Column(
children: [
Image.asset(
AppUi.assets.logo,
height: 600.w,
width: Get.width,
fit: BoxFit.cover,
),
DrawerListTile('الأسئلة', Icons.question_answer, ()
Get.back();
Get.toNamed(Routes.QUESTIONS);
)
],
),
),
backgroundColor: AppUi.colors.appSkyLight,
appBar: TafraAppBar(
titleText: 'الرئيسية',
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
AzkarCard(
'أذكار الصباح',
Icons.wb_sunny,
onTap: () => controller.routeToAzkar(AzkarType.morning),
key: Key('morning'),
),
AzkarCard(
'أذكار المساء',
Icons.nightlight_round,
onTap: () => controller.routeToAzkar(AzkarType.evening),
key: Key('evening'),
),
],
),
],
),
);
DrawerListTile
和 AzkarCard
是自定义小部件。当我删除它们并用Text
小部件交换它们时,测试代码成功!。
所以我不知道我要测试的代码是否不应该有自定义小部件。这在实际项目中是不可能的。
【问题讨论】:
【参考方案1】:问题解决了!
这个问题的原因是在MaterialApp
中缺少ScreenUtilInit
。
pumpWidget
方法应该是这样的:
_pumpWidget(WidgetTester tester,) => tester.pumpWidget(
MaterialApp(
home: ScreenUtilInit(
designSize: Size(1080, 1920), builder: () => HomeView()),
),
);
【讨论】:
感谢您用解决方案回答您自己的问题,为我省去了很多痛苦! 很高兴为您提供帮助 :) 很棒的兄弟。你为我节省了很多时间以上是关于LateInitializationError:字段“_instance@99075166”尚未初始化的主要内容,如果未能解决你的问题,请参考以下文章
LateInitializationError:字段“_userData@32329253”尚未初始化
LateInitializationError:字段尚未在 Flutter 中初始化
LateInitializationError:字段“chatRoomsStream”尚未在 Flutter 中初始化
如何解决 Flutter 中的 LateInitializationError?