alibaba / fish-redux

An assembled flutter application framework.
https://github.com/alibaba/fish-redux
Apache License 2.0
7.33k stars 843 forks source link

在page页面的生命周期 initstate ,展示对话框报错,放在didChangeDependencies同样也报错 #563

Closed andyxiaoxi closed 4 years ago

andyxiaoxi commented 4 years ago

initstate的错误 void _init(Action action, Context ctx) { //展示对话框 DialogUtil.showNetLoadingDialog(ctx.context); //网络请求 _getHouseData(ctx.context, ctx.extra['page'], filterData).then((houseData) { ctx.dispatch(HouseActionCreator.onLoadHouseData(houseData)); }); I/flutter (12789): The following assertion was thrown building _PageWidget<HouseState, Map<String, dynamic>>(state: I/flutter (12789): _PageState<HouseState, Map<String, dynamic>>#d306a): I/flutter (12789): inheritFromWidgetOfExactType(_LocalizationsScope) or inheritFromElement() was called before I/flutter (12789): ComponentState.initState() completed. I/flutter (12789): When an inherited widget changes, for example if the value of Theme.of() changes, its dependent I/flutter (12789): widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor I/flutter (12789): or an initState() method, then the rebuilt dependent widget will not reflect the changes in the I/flutter (12789): inherited widget. I/flutter (12789): Typically references to inherited widgets should occur in widget build() methods. Alternatively, I/flutter (12789): initialization based on inherited widgets can be placed in the didChangeDependencies method, which I/flutter (12789): is called after initState and whenever the dependencies change thereafter. I/flutter (12789): I/flutter (12789): User-created ancestor of the error-causing widget was: I/flutter (12789): MaterialApp file:///C:/ccb-flutter/CCBAgency/lib/main.dart:59:12 I/flutter (12789): I/flutter (12789): When the exception was thrown, this was the stack: I/flutter (12789): #0 StatefulElement.inheritFromElement. (package:flutter/src/widgets/framework.dart:4164:9) I/flutter (12789): #1 StatefulElement.inheritFromElement (package:flutter/src/widgets/framework.dart:4207:6) I/flutter (12789): #2 Element.inheritFromWidgetOfExactType (package:flutter/src/widgets/framework.dart:3439:14) I/flutter (12789): #3 Localizations.of (package:flutter/src/widgets/localizations.dart:448:47)

2, didChangeDependencies 里的错误 void _depend(Action action, Context ctx){ //展示对话框 DialogUtil.showNetLoadingDialog(ctx.context); ErpHouseFilterData filterData = ctx.state.filterData.filterData; //请求房源 _getHouseData(ctx.context, ctx.extra['page'], filterData).then((houseData) { ctx.dispatch(HouseActionCreator.onLoadHouseData(houseData)); }); }

The following assertion was thrown building _PageWidget<HouseState, Map<String, dynamic>>(state: I/flutter (12789): _PageState<HouseState, Map<String, dynamic>>#3d574): I/flutter (12789): setState() or markNeedsBuild() called during build. I/flutter (12789): This Overlay widget cannot be marked as needing to build because the framework is already in the I/flutter (12789): process of building widgets. A widget can be marked as needing to be built during the build phase I/flutter (12789): only if one of its ancestors is currently building. This exception is allowed because the framework I/flutter (12789): builds parent widgets before children, which means a dirty descendant will always be built. I/flutter (12789): Otherwise, the framework might not visit this widget during this build phase. I/flutter (12789): The widget on which setState() or markNeedsBuild() was called was: I/flutter (12789): Overlay-[LabeledGlobalKey#7829d] I/flutter (12789): The widget which was currently being built when the offending call was made was: I/flutter (12789): _PageWidget<HouseState, Map<String, dynamic>> I/flutter (12789): I/flutter (12789): User-created ancestor of the error-causing widget was: I/flutter (12789): MaterialApp file:///C:/ccb-flutter/CCBAgency/lib/main.dart:59:12 I/flutter (12789): I/flutter (12789): When the exception was thrown, this was the stack: I/flutter (12789): #0 Element.markNeedsBuild. (package:flutter/src/widgets/framework.dart:3687:11) I/flutter (12789): #1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3702:6) I/flutter (12789): #2 State.setState (package:flutter/src/widgets/framework.dart:1161:14) I/flutter (12789): #3 OverlayState.insertAll (package:flutter/src/widgets/overlay.dart:346:5) I/flutter (12789): #4 OverlayRoute.install (package:flutter/src/widgets/routes.dart:43:24) I/flutter (12789): #5 TransitionRoute.install (package:flutter/src/widgets/routes.dart:180:11) I/flutter (12789): #6 ModalRoute.install (package:flutter/src/widgets/routes.dart:907:11) I/flutter (12789): #7 NavigatorState.push (package:flutter/src/widgets/navigator.dart:1768:11) I/flutter (12789): #8 showGeneralDialog (package:flutter/src/widgets/routes.dart:1566:53) I/flutter (12789): #9 showDialog (package:flutter/src/material/dialog.dart:705:10) I/flutter (12789): #10 DialogUtil.showNetLoadingDialog (package:ccbagency/util/DialogUtil.dart:23:7) I/flutter (12789): #11 _depend (package:ccbagency/page/house_page/effect.dart:67:14) I/flutter (12789): #12 combineEffects. (package:fish_redux/src/redux_component/helper.dart:74:32) I/flutter (12789): #13 createEffectDispatch. (package:fish_redux/src/redux_component/helper.dart:89:39) I/flutter (12789): #14 createDispatch. (package:fish_redux/src/redux_component/helper.dart:109:39) I/flutter (12789): #15 LogicContext.onLifecycle (package:fish_redux/src/redux_component/context.dart:9

VintLin commented 4 years ago

您说的问题其实和Fish Redux没有太多关系, 这个和Widget的生命周期有有关,在创建一个Widget的过程中initState以及第一次调用的didChangeDependencies时,Widget的context并没有初始化完成,因此在您展示弹窗时才会出现相应的问题。 解决方法: 您可以通过引入:

import 'package:flutter/scheduler.dart';

在iniState中获取页面第一帧创建时的回调, 在其中做弹窗操作即可:

void _initState(Action action, Context<PageState> ctx) {
  SchedulerBinding.instance.addPostFrameCallback((_) {
   // Do Something
  });
}
andyxiaoxi commented 4 years ago

@VoterLin 感谢你的回答,已经解决了这个问题