我正在使用flutter_bloc和bloc软件包来管理状态。
在子页面中,我正在初始化bloc,而在后父页面中,则放置了bloc对象。
当我立即返回父页面时,该时间块抛出错误Unhandled Exception: Bad state: Cannot add new events after calling close
,因为在加载的子页面上,我正在块中调用api。完成api后会触发状态。那时已经有大批物体被抛弃了。
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Bad state: Cannot add new events after calling close
E/flutter (17290): #0 _BroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:249:24)
E/flutter (17290): #1 Subject._add (package:rxdart/src/subjects/subject.dart:124:16)
E/flutter (17290): #2 Subject.add (package:rxdart/src/subjects/subject.dart:118:5)
E/flutter (17290): #3 Bloc._bindStateSubject.<anonymous closure> (package:bloc/src/bloc.dart:90:23)
E/flutter (17290): #4 Stream.forEach.<anonymous closure>.<anonymous closure> (dart:async/stream.dart:814:45)
E/flutter (17290): #5 _runUserCode (dart:async/stream_pipe.dart:11:23)
E/flutter (17290): #6 Stream.forEach.<anonymous closure> (dart:async/stream.dart:814:11)
E/flutter (17290): #7 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (17290): #8 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (17290): #9 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (17290): #10 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (17290): #11 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
E/flutter (17290): #12 _SyncBroadcastStreamController._sendData (dart:async/broadcast_stream_controller.dart:375:20)
E/flutter (17290): #13 _BroadcastStreamController._add (dart:async/broadcast_stream_controller.dart:287:5)
E/flutter (17290): #14 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (17290): #15 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (17290): #16 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (17290): #17 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (17290): #18 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
E/flutter (17290): #19 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:132:11)
E/flutter (17290): #20 _ForwardingStream._handleData (dart:async/stream_pipe.dart:98:10)
E/flutter (17290): #21 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:164:13)
E/flutter (17290): #22 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (17290): #23 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (17290): #24 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (17290): #25 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (17290): #26 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter (17290): #27 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
E/flutter (17290): #28 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:667:7)
E/flutter (17290): #29 _rootRun (dart:async/zone.dart:1120:38)
E/flutter (17290): #30 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (17290): #31 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (17290): #32 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
E/flutter (17290): #33 _rootRun (dart:async/zone.dart:1124:13)
E/flutter (17290): #34 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (17290): #35 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (17290): #36 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
E/flutter (17290): #37 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (17290): #38 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
那么在集团已经处置后如何处理这些?
答案 0 :(得分:0)
为父母和孩子使用通用集团。 调用子级时在父级中创建/初始化块的操作如下:
`BlocProvider.value( value: BlocProvider.of<BlocA>(context), child: ScreenA(), );`
通过上面的写作,您将使集团不再活着, 然后,您可以按以下方式直接访问child中的bloc:
BlocProvider.of<BlocA>(context)
不要将其丢弃在孩子身上, 然后,当您从孩子那里回到父母身边时,您的集团将不会被处置,只会在父母销毁或处置时处置。
所有内容都写在自述文件中: https://pub.dev/packages/flutter_bloc