addPostFrameCallback vs scheduleFrameCallback - flutter

So, I was wondering, when to use WidgetsBinding.instance.addPostFrameCallback and WidgetsBinding.instance.scheduleFrameCallback for setState, as according to the documentation, the first one doesn't initiate a frame on its own, while if we asynchronously call setState then it might get called during the build of another widget, which can cause not to update the other widget, so for it, we add it as a "post frame" callback, but if it is called not during a build, then it will not initiate the next frame on its own, i.e we cannot always wrap async setState calls with it, as there is a good chance it might be called during not a build, that's when I found scheduleFrameCallback, and according to its documentation, I think it schedules the next frame and adds its as a callback to it, i.e it also initiates a frame. So is it always better to call setState inside it, or is it something else?

Related

Is there any way how to reload all FutureBuilders?

Is there any way how to reload all FutureBuilders in application without need to change every futurebuilder?
Yes, there is. Just call setState((){}) in the tree (or subtree) that has all the FutureBuilders and make sure that the future property calls the future method directly, for example. The build method should trigger a new call to the future.

Where should my automatic screen change occur - presentation or model?

I am using scopedModel approach in flutter, but the question still remains with other patterns like flutter.
-My UI calls authModel.login()
-authModel.login does async work and calls notifyListeners
-build is automatically triggered on my UI
-inside build method, UI checks the login state on authModel, then calls
addPostFrameCallback to change the screen from the build method.
Is this the best design? I feel like this is code smell, but I am not sure. Would it be better if the authModel was passed an onLoginCompleteCallback function from the UI that would change the screen, and then I would not need to call addPostFrameCallback in the build method?
The presentation on the screen is not dependant on the isLoggedIn bool at all. Its just observed to change the screen.

How to call a method from InheritedWidget only once?

I'm developing with reactive components streams/observables with Rx_command, Rx_dart
Problem:
In my Flutter app I have inherited widget that can be called anywhere with:
FooProvider.of(context).foo.method1...
I need to make a first call to the method when the UI loads at first time
I can't use init.state because it's impossible
I use didchangedependencies it works but..
... every time the ui reloads, the didchangedependencies is called and the method is executed once again.
I don't want it to be executed and I can't use init.state
How can execute the method only once?
Instead of context.inheritFromWidgetOfExactType, use context.ancestorInheritedElementForWidgetOfExactType
final myInherited = context.ancestorInheritedElementForWidgetOfExactType(MyInherited)?.widget;
This method is available inside initState

How should I tell flutter that my app needs rebuilding externally?

I want my app to get an intent, and depending on the intent to display something in the app.
Normally, when a widget depends on state, you put it in a State and run setState().
The issue is that when I try starting my flutter app with different intents, I just get I/FlutterActivityDelegate( 4472): onResume setting current activity to this. And in a way it makes sense - I'm not saying anywhere in the flutter code that my widget needs repainting - since I get my intent through Java.
On the other hand, there should be a way to tell flutter to repaint my widget by intent? Or is there something else I should do?
Specifically to get notified about onResume event you can use WidgetsBindingObserver. Implement its didChangeAppLifecycleState method and respond to AppLifecycleState.resumed by doing whatever it is you want the UI to do, such as call setState to trigger rebuild. That said, there needs to be an effective change for the UI to be repainted. Simply calling setState with no effective state change may not result (in fact, should not) in actual UI changes.
In general, you can send a message from Java (or Objective-C/Swift) to Flutter using BasicMessageChannel and make your app react to the message (e.g. call setState or schedule frames).

React Native: what lifecycle should I look for when I want to refresh ListView whenever I navigate back to it?

Pretty much like the official tutorial here https://facebook.github.io/react-native/docs/tutorial.html:
I have the fetchData method called in ComponentDidMount, but according to this, ComponentDidMountwould only be called once after initial rendering of the component. So should I do call the same fetchData everytime before I navigate back to this ListView, and pass the updated data as props to the ListView, and then update the listView's state parameter accordingly in componentDidUpdate? Or is there any better way to do this?
You should register an event listener on componentDidMount and unregister it on componentWillUnmout, and emit events whenever and wherever you change the data your component displays.
Good rundown of this pattern here: https://colinramsay.co.uk/2015/07/04/react-native-eventemitters.html