Flutter-加载CircularProgressIndicator时发生异常

问题描述 投票:0回答:1

我在小部件开始时加载指标时遇到问题。以我为例,我有一些数据异步下载到initState中,然后setState将_loading设置为false。

我想通过对话框导航以使用Navigator进行显示和消失。

错误:

The following assertion was thrown building NotificationView(dirty, dependencies: [_LocalizationsScope-[GlobalKey#e97a5], _InheritedTheme], state: _NotificationStateView#5dbc7):
setState() or markNeedsBuild() called during build.

This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: Overlay-[LabeledGlobalKey<OverlayState>#e85f9]
  state: OverlayState#340b4(entries: [OverlayEntry#2f82c(opaque: true; maintainState: false), OverlayEntry#2187c(opaque: false; maintainState: true), OverlayEntry#50b38(opaque: false; maintainState: false), OverlayEntry#a3933(opaque: false; maintainState: true), OverlayEntry#3e2d8(opaque: false; maintainState: false), OverlayEntry#7c2c7(opaque: false; maintainState: true)])
The widget which was currently being built when the offending call was made was: NotificationView
  dirty
  dependencies: [_LocalizationsScope-[GlobalKey#e97a5], _InheritedTheme]
  state: _NotificationStateView#5dbc7
The relevant error-causing widget was: 
  NotificationView lib/main.dart:51:38
When the exception was thrown, this was the stack: 
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:3896:11)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3911:6)
#2      State.setState (package:flutter/src/widgets/framework.dart:1168:14)
#3      OverlayState.insertAll (package:flutter/src/widgets/overlay.dart:344:5)
#4      OverlayRoute.install (package:flutter/src/widgets/routes.dart:44:24)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ (2) Exception caught by widgets library ═══════════════════════════════════════════════════
The method 'drive' was called on null.
Receiver: null
Tried calling: drive<Color>(Instance of '_ChainedEvaluation<Color>')
The relevant error-causing widget was: 
  MaterialApp lib/main.dart:14:10
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ (3) Exception caught by widgets library ═══════════════════════════════════════════════════
'package:flutter/src/animation/animations.dart': Failed assertion: line 376 pos 15: 'parent != null': is not true.
The relevant error-causing widget was: 
  MaterialApp lib/main.dart:14:10
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ (4) Exception caught by widgets library ═══════════════════════════════════════════════════
'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1782 pos 12: '!_debugLocked': is not true.
The relevant error-causing widget was: 
  NotificationView lib/main.dart:51:38
════════════════════════════════════════════════════════════════════════════════════════════════════

初始化视图

Widget _buildView(BuildContext context) {
    if (_loading) {
      return showIndicator(context);
    }

    return Container(....);
}

指标方法

showIndicator (BuildContext context) {
  return showDialog(
    context: context,
    child: Center(
      child: CircularProgressIndicator(),
    ),
  );
}
flutter dart flutter-layout flutter-animation indicator
1个回答
0
投票

使用FutureBuilder进行异步操作,因此当您完成将来的操作时,您将在快照中获取数据,否则将返回CircularProgressIndicator。发生此错误的原因是您在构建方法完成之前调用了setState。

© www.soinside.com 2019 - 2024. All rights reserved.