我正在开发一个应用程序(类似于用于计算的测验应用程序)。对于每个屏幕,我都有一个控制器,其中一个屏幕是结果屏幕。在第一次尝试中一切正常,但是,在第二次尝试中,当我完成测验时,结果屏幕返回 null 并说“在 null 值上使用了 null 检查运算符。
这是抛出的异常
════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building _BodyBuilder:
Null check operator used on a null value
The relevant error-causing widget was
Scaffold
lib/…/view/result_screen.dart:27
When the exception was thrown, this was the stack
#0 GetBuilderState.initState
package:get/…/simple/get_state.dart:134
#1 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4770
#2 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4605
... Normal element mounting (19 frames)
#21 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3662
#22 MultiChildRenderObjectElement.inflateWidget
package:flutter/…/widgets/framework.dart:6285
#23 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:6296
... Normal element mounting (111 frames)
#134 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3662
#135 MultiChildRenderObjectElement.inflateWidget
package:flutter/…/widgets/framework.dart:6285
#136 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:6296
... Normal element mounting (9 frames)
#145 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3662
#146 MultiChildRenderObjectElement.inflateWidget
package:flutter/…/widgets/framework.dart:6285
#147 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:6296
... Normal element mounting (170 frames)
#317 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3662
#318 MultiChildRenderObjectElement.inflateWidget
package:flutter/…/widgets/framework.dart:6285
#319 Element.updateChild
package:flutter/…/widgets/framework.dart:3414
#320 RenderObjectElement.updateChildren
package:flutter/…/widgets/framework.dart:5717
#321 MultiChildRenderObjectElement.update
package:flutter/…/widgets/framework.dart:6308
#322 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#323 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4657
#324 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4805
#325 Element.rebuild
package:flutter/…/widgets/framework.dart:4322
#326 StatefulElement.update
package:flutter/…/widgets/framework.dart:4837
#327 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#328 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4657
#329 Element.rebuild
package:flutter/…/widgets/framework.dart:4322
#330 ProxyElement.update
package:flutter/…/widgets/framework.dart:4985
#331 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#332 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4657
#333 Element.rebuild
package:flutter/…/widgets/framework.dart:4322
#334 ProxyElement.update
package:flutter/…/widgets/framework.dart:4985
#335 _InheritedNotifierElement.update
package:flutter/…/widgets/inherited_notifier.dart:181
#336 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#337 SingleChildRenderObjectElement.update
package:flutter/…/widgets/framework.dart:6154
#338 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#339 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4657
#340 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4805
#341 Element.rebuild
package:flutter/…/widgets/framework.dart:4322
#342 StatefulElement.update
package:flutter/…/widgets/framework.dart:4837
#343 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#344 SingleChildRenderObjectElement.update
package:flutter/…/widgets/framework.dart:6154
#345 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#346 SingleChildRenderObjectElement.update
package:flutter/…/widgets/framework.dart:6154
#347 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#348 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4657
#349 Element.rebuild
package:flutter/…/widgets/framework.dart:4322
#350 ProxyElement.update
package:flutter/…/widgets/framework.dart:4985
#351 Element.updateChild
package:flutter/…/widgets/framework.dart:3401
#352 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4657
#353 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4805
#354 Element.rebuild
package:flutter/…/widgets/framework.dart:4322
#355 BuildOwner.buildScope
package:flutter/…/widgets/framework.dart:2619
#356 WidgetsBinding.drawFrame
package:flutter/…/widgets/binding.dart:878
#357 RendererBinding._han
#358 SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:1143
#359 SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:1080
#360 SchedulerBinding._handleDrawFrame
package:flutter/…/scheduler/binding.dart:996
#364 _invoke (dart:ui/hooks.dart:163:10)
#365 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:259:5)
#366 _drawFrame (dart:ui/hooks.dart:126:31)
(elided 3 frames from dart:async)
当我单击错误堆栈中的 ( lib/.../view/result_screen.dart:27 ) 时,它会将我带到屏幕的脚手架。我不确定为什么会这样。
我有一个 HomeWrapper 屏幕,其中包含一些小部件和一个页面视图
void initControllers() {
Get.put(HomeScreenController());
Get.put(FirstPageController());
Get.put(SecondPageController());
Get.put(ThirdPageController());
Get.put(FourthPageController());
Get.put(FifthPageController());
Get.put(SixthPageController());
Get.put(SeventhPageController());
Get.put(EighthPageController());
Get.put(ResultsController());
}
@override
Widget build(BuildContext context) {
initControllers();
return Column(...);
}
在第八页,有一个按钮,执行一个方法来计算结果并导航到结果屏幕
class FinishButton extends StatelessWidget {
final bool _isValid = Get.find<FirstPageController>().validLandArea;
@override
Widget build(BuildContext context) {
return GetBuilder<ResultsController>(
builder: (controller) => Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: CupertinoButton(
child: Text('Finish'),
onPressed: () {
if (_isValid) {
controller.clickFinishButton();
Navigator.pushReplacementNamed(context, ResultScreen.id);
} else {
HomeScreenController.to.goToLandArea();
}
},
color: AppColors.primaryColor,
),
),
);
}
}
在结果页面中,有一个按钮可以让我返回 Root,它将根据用户值(无论他是否登录)返回 HomeWrapper 或 Authetication
CupertinoButton(
child: Text('Back to Home'),
onPressed: () {
Navigator.pushReplacementNamed(context, Root.id);
},
color: AppColors.primaryColor,
),
当我回到 HomeWrapper 时,所有页面都可以使用它们的控制器正常工作,除非我导航回结果屏幕。
我试图让控制器永久和懒惰地放置,但没有奏效。如果有人可以告诉我问题是什么。