var filterQuery =algolia.instance.index(indexName.value.toString()).facetQuery("cast");
I am trying to use facetQuery method of Algolia package, but I am facing an error as shown below.
The code shown above is causing this error:
E/flutter (24259): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Bad state: Cannot set the body fields of a Request with content-type "application/json".
E/flutter (24259): #0 Request.bodyFields= (package:http/src/request.dart:133:7)
E/flutter (24259): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:87:17)
E/flutter (24259): #2 BaseClient.post (package:http/src/base_client.dart:32:7)
E/flutter (24259): #3 post.<anonymous closure> (package:http/http.dart:69:16)
E/flutter (24259): #4 _withClient (package:http/http.dart:164:20)
E/flutter (24259): #5 post (package:http/http.dart:68:5)
E/flutter (24259): #6 AlgoliaIndexReference.facetQuery (package:algolia/src/index_reference.dart:81:31)
E/flutter (24259): #7 ProductController.fetchProducts (package:project/screens/searc_for_all/controller/search_for_all_controller.dart:50:74)
E/flutter (24259): #8 ProductController.onInit (package:project/screens/searc_for_all/controller/search_for_all_controller.dart:21:5)
E/flutter (24259): #9 GetLifeCycleBase._onStart (package:get/get_instance/src/lifecycle.dart:66:5)
E/flutter (24259): #10 InternalFinalCallback.call (package:get/get_instance/src/lifecycle.dart:12:26)
E/flutter (24259): #11 GetInstance._startController (package:get/get_instance/src/get_instance.dart:253:16)
E/flutter (24259): #12 GetInstance._initDependencies (package:get/get_instance/src/get_instance.dart:204:11)
E/flutter (24259): #13 GetInstance.find (package:get/get_instance/src/get_instance.dart:301:17)
E/flutter (24259): #14 GetInstance.put (package:get/get_instance/src/get_instance.dart:86:12)
E/flutter (24259): #15 Inst.put (package:get/get_instance/src/extension_instance.dart:89:21)
E/flutter (24259): #16 new _SearchForAllState (package:project/screens/searc_for_all/searchForAll.dart:37:51)
E/flutter (24259): #17 SearchForAll.createState (package:project/screens/searc_for_all/searchForAll.dart:34:39)
E/flutter (24259): #18 new StatefulElement (package:flutter/src/widgets/framework.dart:4842:25)
E/flutter (24259): #19 StatefulWidget.createElement (package:flutter/src/widgets/framework.dart:770:38)
E/flutter (24259): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3785:40)
E/flutter (24259): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
E/flutter (24259): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6284:14)
E/flutter (24259): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
E/flutter (24259): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
E/flutter (24259): #25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
E/flutter (24259): #26 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
E/flutter (24259): #27 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735:5)
E/flutter (24259): #28 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729:5)
E/flutter (24259): #29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
E/flutter (24259): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
E/flutter (24259): #31 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6284:14)
E/flutter (24259): #32 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
E/flutter (24259): #33 Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
E/flutter (24259): #34 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6284:14)
E/flutter (24259): #35 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
E/flutter (24259): #36 Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
E/flutter (24259): #37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
E/flutter (24259): #38 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
E/flutter (24259): #39 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
E/flutter (24259): #40 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735:5)
E/flutter (24259): #41 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4919:11)
E/flutter (24259): #42 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729:5)
E/flutter (24259): #43 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
E/flutter (24259): #44 Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
E/flutter (24259): #45 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6284:14)
on tap submit subbmit method call .
[1]: https://i.stack.imgur.com/glYah.png
error :
successful
E/flutter (11575): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The method 'readAsBytesSync' was called on null.
E/flutter (11575): Receiver: null
E/flutter (11575): Tried calling: readAsBytesSync()
E/flutter (11575): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter (11575): #1 _Delivery_DetailsState.submitForm (package:shop_me/src/screens/product_delivery/ui/delivery_detail.dart:500:37)
E/flutter (11575): #2 _Delivery_DetailsState.build. (package:shop_me/src/screens/product_delivery/ui/delivery_detail.dart:384:33)
E/flutter (11575): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21)
E/flutter (11575): #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (11575): #5 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:607:11)
E/flutter (11575): #6 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
E/flutter (11575): #7 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:267:7)
E/flutter (11575): #8 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
E/flutter (11575): #9 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:443:20)
E/flutter (11575): #10 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
E/flutter (11575): #11 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:287:11)
E/flutter (11575): #12 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
E/flutter (11575): #13 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
E/flutter (11575): #14 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
E/flutter (11575): #15 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
E/flutter (11575): #16 _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter (11575): #17 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (11575): #18 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (11575): #19 _invoke1 (dart:ui/hooks.dart:182:10)
E/flutter (11575): #20 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:282:7)
E/flutter (11575): #21 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
E/flutter (11575):
I am trying to use the signInWithGoogle method from Auth package.
https://pub.dev/packages/auth
I have the following exception and stack trace.
I/flutter (20513): Caught: PlatformException(exception, Unknown signInOption, null)
I/flutter (20513): Stack: #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
I/flutter (20513): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
I/flutter (20513): <asynchronous suspension>
I/flutter (20513): #2 MethodChannelGoogleSignIn.init (package:google_sign_in_platform_interface/src/method_channel_google_sign_in.dart:26:20)
I/flutter (20513): #3 GoogleSignIn._ensureInitialized (package:google_sign_in/google_sign_in.dart:228:62)
I/flutter (20513): #4 GoogleSignIn._callMethod (package:google_sign_in/google_sign_in.dart:210:11)
I/flutter (20513): <asynchronous suspension>
I/flutter (20513): #5 GoogleSignIn._addMethodCall (package:google_sign_in/google_sign_in.dart:266:18)
I/flutter (20513): <asynchronous suspension>
I/flutter (20513): #6 GoogleSignIn.signIn (package:google_sign_in/google_sign_in.dart:336:9)
I/flutter (20513): #7 Auth.signInWithGoogle (package:auth/auth.dart:686:43)
I/flutter (20513): #8 _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
I/flutter (20513): #9 Auth.signInWithGoogle (package:auth/auth.dart:674:32)
I/flutter (20513): #10 _LoginPageState.build.<anonymous closure> (package:betting_ai/l
Here is the gist that I use:
https://gist.github.com/deadsoul44/f95c72c3d7a3529d4ac2efce751a3866
Any idea how to solve this issue?
Actually I am working on Login page with Bloc. So when I press submit button, it does business logic on bloc then trying to navigate to home page based on the result. I tried this
Widget submitButton() {
return StreamBuilder(
stream: bloc.submitStream,
builder: (context, snapshot) {
if (snapshot.data == "Success") {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RegisterPage(),
),
);
}
return RaisedButton(
child: Text("Submit"),
onPressed: () {
bloc.submitSink.add(null);
},
);
},
);
}
But I got error like this The widget which was currently being built when the offending call was made....
Later then I found two solution:
1. Using SchedulerBinding,
2. Using WidgetsBinding. So what is the difference between SchedulerBinding and WidgetsBinding. Which one do I have to use?
Full Error Log:
I/flutter (17893): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (17893): The following assertion was thrown building LayoutBuilder:
I/flutter (17893): setState() or markNeedsBuild() called during build.
I/flutter (17893): This Overlay widget cannot be marked as needing to build because the framework is already in the
I/flutter (17893): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (17893): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (17893): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (17893): Otherwise, the framework might not visit this widget during this build phase.
I/flutter (17893): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (17893): Overlay-[LabeledGlobalKey<OverlayState>#70089](state: OverlayState#3bf13(entries:
I/flutter (17893): [OverlayEntry#9280a(opaque: false; maintainState: false), OverlayEntry#1af87(opaque: false;
I/flutter (17893): maintainState: true), OverlayEntry#9fb49(opaque: false; maintainState: false),
I/flutter (17893): OverlayEntry#87589(opaque: false; maintainState: true)]))
I/flutter (17893): The widget which was currently being built when the offending call was made was:
I/flutter (17893): LayoutBuilder(renderObject: _RenderLayoutBuilder#d3baf relayoutBoundary=up1 NEEDS-LAYOUT
I/flutter (17893): NEEDS-PAINT)
I/flutter (17893):
I/flutter (17893): When the exception was thrown, this was the stack:
I/flutter (17893): #0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:3497:11)
I/flutter (17893): #1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3523:6)
I/flutter (17893): #2 State.setState (package:flutter/src/widgets/framework.dart:1138:14)
I/flutter (17893): #3 OverlayState.insertAll (package:flutter/src/widgets/overlay.dart:301:5)
I/flutter (17893): #4 OverlayRoute.install (package:flutter/src/widgets/routes.dart:43:24)
I/flutter (17893): #5 TransitionRoute.install (package:flutter/src/widgets/routes.dart:184:11)
I/flutter (17893): #6 ModalRoute.install (package:flutter/src/widgets/routes.dart:899:11)
I/flutter (17893): #7 NavigatorState.push (package:flutter/src/widgets/navigator.dart:1672:11)
I/flutter (17893): #8 Navigator.push (package:flutter/src/widgets/navigator.dart:1011:34)
I/flutter (17893): #9 _SetPinPageState.submitButton (package:technicalreport/screens/set_pin_page.dart:94:15)
I/flutter (17893): #10 _SetPinPageState.build.<anonymous closure>.<anonymous closure> (package:technicalreport/screens/set_pin_page.dart:52:29)
I/flutter (17893): #11 OrientationBuilder._buildWithConstraints (package:flutter/src/widgets/orientation_builder.dart:48:12)
I/flutter (17893): #12 _LayoutBuilderElement._layout.<anonymous closure> (package:flutter/src/widgets/layout_builder.dart:111:26)
I/flutter (17893): #13 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2253:19)
I/flutter (17893): #14 _LayoutBuilderElement._layout (package:flutter/src/widgets/layout_builder.dart:107:11)
I/flutter (17893): #15 RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1728:58)
I/flutter (17893): #16 PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:797:15)
I/flutter (17893): #17 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1728:13)
I/flutter (17893): #18 _RenderLayoutBuilder.performLayout (package:flutter/src/widgets/layout_builder.dart:205:5)
I/flutter (17893): #19 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #20 MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:142:11)
I/flutter (17893): #21 _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:350:7)
I/flutter (17893): #22 MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:212:7)
I/flutter (17893): #23 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:356:14)
I/flutter (17893): #24 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #25 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #26 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #27 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #28 _RenderCustomClip.performLayout (package:flutter/src/rendering/proxy_box.dart:1206:11)
I/flutter (17893): #29 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #30 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #31 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #32 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #33 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #34 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #35 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #36 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #37 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #38 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #39 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #40 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #41 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #42 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #43 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #44 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #45 RenderOffstage.performLayout (package:flutter/src/rendering/proxy_box.dart:3032:13)
I/flutter (17893): #46 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #47 RenderStack.performLayout (package:flutter/src/rendering/stack.dart:510:15)
I/flutter (17893): #48 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #49 __RenderTheatre&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #50 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #51 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #52 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #53 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #54 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #55 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #56 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #57 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (17893): #58 RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (17893): #59 RenderView.performLayout (package:flutter/src/rendering/view.dart:151:13)
I/flutter (17893): #60 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1507:7)
I/flutter (17893): #61 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:766:18)
I/flutter (17893): #62 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:329:19)
I/flutter (17893): #63 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:701:13)
I/flutter (17893): #64 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:268:5)
I/flutter (17893): #65 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:988:15)
I/flutter (17893): #66 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:928:9)
I/flutter (17893): #67 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:749:7)
I/flutter (17893): #76 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
I/flutter (17893): #77 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
I/flutter (17893): #78 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter (17893): (elided 8 frames from package dart:async)
I/flutter (17893): ════════════════════════════════════════════════════════════════════════════════════════════════════
D/ (17893): HostConnection::get() New Host Connection established 0xa0ba6b40, tid 17931
D/EGL_emulation(17893): eglMakeCurrent: 0xa3508c40: ver 2 0 (tinfo 0x9084ab40)
There are three callbacks available in flutter which rendering flutter widgets.
Transient callbacks: triggered by the system’s [Window.onBeginFrame] callback, for synchronizing the application’s behavior to the system’s display. For example, [Ticker]s and [AnimationController]s trigger from these.
Persistent callbacks: triggered by the system’s [Window.onDrawFrame] callback, for updating the system’s display after transient callbacks have executed. For example, the rendering layer uses this to drive its rendering pipeline.
Post-frame callbacks: which are run after persistent callbacks, just before returning from the [Window.onDrawFrame] callback. * Non-rendering tasks, to be run between frames. These are given a priority and are executed in priority order according to a schedulingStrategy.
All above callbacks run sequentially, but for us, the last callback would work i.e Post-frame callbacks
Widgets Binding: The glue between the widgets layer and the Flutter engine. which comes with WidgetsBindingObserver mixin which has many callbacks but one which required to use is didChangeAppLifecycleState that returns AppLifecycleState.
Scheduler Binding: This is also similar to WidgetBinding but it does not provide any life cycle callbacks.
The below binding example would be called exactly the ones, means print statement like WidgetsBinding & SchedulerBinding will be printed only once as we called it initState(), but it will be called when build method finished it’s rendering.
void initState() {
super.initState();
print("initState");
WidgetsBinding.instance.addPostFrameCallback((_) {
print("WidgetsBinding");
});
SchedulerBinding.instance.addPostFrameCallback((_) {
print("SchedulerBinding");
});
}
Both solutions do the same.
WidgetsBinding inherits this Method from SchedulerBinding
https://api.flutter.dev/flutter/widgets/WidgetsBinding-mixin.html
It registers a callback that will be called after the frame is build.
See in the docs:
https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html
I am using flutter_bloc & bloc package to manage state.
In child page i am initialize bloc & while back parent page dispose the bloc object.
When i comes back immediately on parent page that time bloc throw error Unhandled Exception: Bad state: Cannot add new events after calling close because on child page on load i am calling api in bloc. which trigger state after completed api. At that time already bloc object disposed.
[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)
So how handle these when bloc already disposed ?
Use common bloc for parent and child.
Create/Initialise bloc in parent when calling child do as following:
`BlocProvider.value( value: BlocProvider.of<BlocA>(context), child: ScreenA(), );`
By writing above you will keep alive the bloc from dispose,
Then you can simply access bloc in child as following:
BlocProvider.of<BlocA>(context)
Just don't dispose it in child,
And then, when you go back to parent from child your bloc won't get disposed and it will only dispose when parent destroy or dispose.
Everything is written there in readme:
https://pub.dev/packages/flutter_bloc