问题描述
我是陌生的,正在尝试在应用程序中实现bloc。我想通过事件更改标志的值,并在标志的布尔值更改后刷新状态。 我试图在博客侦听器中设置setState(),但返回以下异常。
[ERROR:Flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: setState() or markNeedsBuild() called during build.
E/Flutter ( 2570): 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.
E/Flutter ( 2570): The widget on which setState() or markNeedsBuild() was called was:
E/Flutter ( 2570): Overlay-[LabeledGlobalKey<OverlayState>#94b3b]
E/Flutter ( 2570): The widget which was currently being built when the offending call was made was:
E/Flutter ( 2570): ActivitySignUpWidget
E/Flutter ( 2570): #0 Element.markNeedsBuild.<anonymous closure> (package:Flutter/src/widgets/framework.dart:4211:11)
E/Flutter ( 2570): #1 Element.markNeedsBuild (package:Flutter/src/widgets/framework.dart:4226:6)
E/Flutter ( 2570): #2 State.setState (package:Flutter/src/widgets/framework.dart:1260:14)
E/Flutter ( 2570): #3 OverlayState.insert (package:Flutter/src/widgets/overlay.dart:326:5)
E/Flutter ( 2570): #4 ToastView.createView (package:toast/toast.dart:67:18)
E/Flutter ( 2570): #5 Toast.show (package:toast/toast.dart:19:15)
E/Flutter ( 2570): #6 _ActivitySignUpState._selectCountrCode (package:crm_application_devops/signup/ActivitySignUp.dart:471:11)
E/Flutter ( 2570): #7 _ActivitySignUpState._createForm (package:crm_application_devops/signup/ActivitySignUp.dart:219:53)
E/Flutter ( 2570): #8 _ActivitySignUpState.build (package:crm_application_devops/signup/ActivitySignUp.dart:115:42)
E/Flutter ( 2570): #9 StatefulElement.build (package:Flutter/src/widgets/framework.dart:4663:28)
E/Flutter ( 2570): #10 ComponentElement.performRebuild (package:Flutter/src/widgets/framework.dart:4546:15)
E/Flutter ( 2570): #11 StatefulElement.performRebuild (package:Flutter/src/widgets/framework.dart:4719:11)
E/Flutter ( 2570): #12 Element.rebuild (package:Flutter/src/widgets/framework.dart:4262:5)
E/Flutter ( 2570): #13 BuildOwner.buildScope (package:Flutter/src/widgets/framework.dart:2667:33)
E/Flutter ( 2570): #14 WidgetsBinding.drawFrame (package:Flutter/src/widgets/binding.dart:866:20)
E/Flutter ( 2570): #15 RendererBinding._handlePersistentFrameCallback (package:Flutter/src/rendering/binding.dart:286:5)
E/Flutter ( 2570): #16 SchedulerBinding._invokeFrameCallback (package:Flutter/src/scheduler/binding.dart:1117:15)
E/Flutter ( 2570): #17 SchedulerBinding.handleDrawFrame (package:Flutter/src/scheduler/binding.dart:1056:9)
E/Flutter ( 2570): #18 SchedulerBinding._handleDrawFrame (package:Flutter/src/scheduler/binding.dart:972:5)
E/Flutter ( 2570): #19 _rootRun (dart:async/zone.dart:1190:13)
E/Flutter ( 2570): #20 _CustomZone.run (dart:async/zone.dart:1093:19)
E/Flutter ( 2570): #21 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/Flutter ( 2570): #22 _invoke (dart:ui/hooks.dart:253:10)
E/Flutter ( 2570): #23 _drawFrame (dart:ui/hooks.dart:211:3)
E/Flutter ( 2570):
@override
Widget build(BuildContext context) {
signupBloc = BlocProvider.of<SignUpBloc>(context);
return Scaffold(
body: Container(
color: Colors.white,child: SafeArea(
child: Column(children: [
Column(
children: [
BlocListener<SignUpBloc,SignUpState>(
listener: (context,state) {
if(state is ContryCodeChangeState){
print("State called");
setState(() {
valueOfInput = state.getContryCodeValue;
});
}
},child: Container(
child: Column(
children: [
_signUpHeader(),Padding(
padding: const EdgeInsets.only(top: 30.0),child: jsonStringValues != null
? Text(
jsonStringValues[
"Sign_up_to_become_a_member"],style: TextStyle(
color: Colors.purpleAccent,fontSize: 16.0,fontWeight: FontWeight.bold),)
: null,),_createAccount(),Center(
child: Padding(
padding: EdgeInsets.only(right: 30,left: 30),child: Visibility(
visible: true,child: _createForm(),))),Padding(
padding: const EdgeInsets.only(top: 16),child: Align(
alignment: FractionalOffset.bottomCenter,child: Text(
"By sign up you are agreeing to th T&C",style:
TextStyle(color: Colors.black,fontSize: 11),],]),);
}
解决方法
if(mounted){
//call set state here
}