What are dependencies of a state in flutter - flutter

I am trying to understand the method didChangeDependencies and according to the definition
It is Called when a dependency of this State object changes.
what do they mean by this. My guess is when properties in the state you manage per screen change. Am I right. Please I would love to understand better.

Try to think of it the same as initState but just a little later.
initstate is called before the state loads its dependencies and for that reason no context is available and you get an error for that if u use context in initstate. However, didChangeDependencies is called just few moments after the state loads its dependencies and context is available at this moment so here you can use context.

In case BuildContext.dependOnInheritedWigetOfExactType called, or in case the widget is been moved inside of the element tree, didChangeDependencies will is always called.

Related

What is init state in flutter?

I have seen in various files about init method under some classes, why we use this method and please also describe other useful methods if you know, Thanks
I have initialized this method but it didn't work properly.
To understand the methods and structure of flutter widgets, you need to understand the lifecycle of flutter screen or flutter application.
So, basically flutter's stateful widgets have really good methods which are
CreateState()
initState()
didChangeDependencies()
build()
deactivate()
dispose()
You can learn more about this on medium
The #initState method is called by Flutter when a widget is inserted into the tree. It's important to note that the initState method is only called once, when the widget is inserted into the tree, and not every time the widget is updated. If you need to perform an operation every time the widget is updated, you can use the didUpdateWidget method instead.
dispose, build, setState, build and much more method are interesting in Flutter, you can try to understand what each method do in this amazing "cookbook" in flutter documentation.
https://docs.flutter.dev/cookbook

Flutter - What is the difference between dispose() and removeListener()?

I don't completely understand the difference between the both methods and when to use them, anyone help me please?
dispose: The framework calls this method when this State object will never build again. After the framework calls dispose, the State object is considered unmounted, and the mounted property is false. It is an error to call setState at this point. This stage of the lifecycle is terminal: there is no way to remount a State object that has been disposed.
You can find more about it here.
removeListener: Remove a previously registered closure from the list of closures that are notified when the object changes.
removeListener must not be called after dispose has been called.
listener is a widget that calls callbacks in response to common pointer events.
More and ref : dispose, removeListener, listener and SO Question on dispose.

Provider - Selector rebuild when scrolling

I am using the Selector widget as shown below
Nothing wrong with it's build method, only call it when the value changes.
But when I use Devtools or android studio to track widget rebuild it's showing that the Selector it self rebuild when I am scrolling whether in a list or any other widget that support scrolling.
Yes the Selector didn't call the build method until the value changes but is this normal ?
Using Devtools:
As you can see the others (2) Selectors doesn't have to be triggers but yet they are.
sorry for my bad English, I can explain in another way in the comment section if you didn't understand me and thanks in advance.
edit:
I guess I know why the selector is rebuilding it's self, because I am using the provider class as listener to scroll controller direction with changenotifier.
here the code
in provider class:
bool isHideHomeScreenTabBar = false;
void hideShowTabBar(ScrollDirection scrollDirection) {
isHideHomeScreenTabBar = scrollDirection == ScrollDirection.reverse;
notifyListeners();
}
in my Home screen:
_scrollController.addListener(() {
Provider.of<AppProvider>(context, listen: false).hideShowTabBar(
_scrollController.position.userScrollDirection);
});
So basically the provider trigger changenotifier with every scroll I do and the selector get notified and rebuild it's self but if the value didn't change the selector won't trigger the build method (so it works fine for the child and the widget in the build method of the selector).
But even so is this normal ? and why, The other selectors aren't even listening to the scroll direction.
Anyway I found an alternative way to do this (with an animation controller) but it would be nice if someone could explain what is happening here, it's important for me at least because I might use another state management.
I know what was happing.
I am using 1 class for the provider that contains all the values I need with many methods using notifyListeners, however I thought it's ok to use 1 provider class if I use Selector for every value I had so anything that need rebuild will only rebuild when it's need it.
The problem with this approach is that with every notifyListeners call every selector got notified and rebuild it self (in my case when any scrolling detected) but the selector doesn't call the builder if the value not changed.
The fix is to make some condition that check the old value and the new value before calling notifyListeners, that works prefect in my case, this decrease the rebuilding happing when I scroll to only 1 as it's intended to be, however the other selectors in the same class also rebuild (I guess because they are all in the same class so every notifyListeners call effect them).
At the end if you end up with similar issue it is better to use ProxyProvider or any way that let you use multiple providres, beside the benefit of a better project architecture and disposing it is way better to have more control over the state.
Thanks to Rémi Rousselet Riverpod it's way better than ProxyProvider and I am using it and it's awesome so consider Riverpod if you want to use ProxyProvider.

When does my class need a build() method?

I understand the general approach to building UI layouts using Flutter. However, I'm still unclear which classes or UI widgets require a .build() method when I'm creating my own vs. using the defaults generated by the project.
I haven't found a clear explanation yet - even in the Flutter tutorials. They all seem to just gloss over how "the build method takes a BuildContext" and then go on to the next subject without explaining further.
Does anyone have a succinct explanation of the build method and when it is or isn't needed? And more specifically: what does it actually do?
build() method describes the part of the user interface represented by this widget.
The framework calls this method in a number of different situations:
After calling initState.
After calling didUpdateWidget.
After receiving a call to setState.
After a dependency of this State object changes (e.g., an InheritedWidget referenced by the previous build changes).
After calling deactivate and then reinserting the State object into the tree at another location.
You can find more Here
TLDR: The build methode is used to create a new widget tree by placing the Widget reurned in the page tree. This method is essentially called when you create or update the widget (by calling setState((){})

What is initState and super.initState in flutter?

In the documentation it is written but I am not able to understand it.
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).
If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then the State should subscribe to that object during initState, unsubscribe from the old object and subscribe to the new object when it changes in didUpdateWidget, and then unsubscribe from the object in dispose.
You cannot use BuildContext.inheritFromWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.inheritFromWidgetOfExactType can be used there.
If you override this, make sure your method starts with a call to super.initState().
But I'm not sure about its meaning. Can you explain it?
Credit to #Remi, initState() is a method which is called once when the stateful widget is inserted in the widget tree.
We generally override this method if we need to do some sort of initialisation work like registering a listener because, unlike build(), this method is called once.
And to unregister your listener (or doing some post work), you override dispose()method.
From here
A subclass of State can override initState to do work that needs to happen just once. For example, override initState to configure animations or to subscribe to platform services. Implementations of initState are required to start by calling super.initState
When a state object is no longer needed, the framework calls dispose() on the state object. Override the dispose function to do cleanup work. For example, override dispose to cancel timers or to unsubscribe from platform services. Implementations of dispose typically end by calling super.dispose
Uses of initState()
initState() is a method of class State and it is considered as an important lifecycle method in Flutter. initState() is called only Once and we use it for one time initializations.
Example :
To initialize data that depends on the specific BuildContext.
To initialize data that needs to executed before build().
Subscribe to Streams.
Thank you for the answers, but I will also reiterate what the guys above have state
#overrride
initState() { // this is called when the class is initialized or called for the first time
super.initState(); // this is the material super constructor for init state to link your instance initState to the global initState context
}
Please allow me to quote the content written by others. I think his explanation is excellent.
https://www.geeksforgeeks.org/flutter-initstate/
There are two types of widgets provided in Flutter.
The Stateless Widget
The Stateful Widget
As the name suggests Stateful Widgets are made up of some ‘States’. The initState() is a method that is called when an object for your stateful widget is created and inserted inside the widget tree. It is basically the entry point for the Stateful Widgets. initState() method is called only and only once and is used generally for initializing the previously defined variables of the stateful widget. initState() method is overridden mostly because as mentioned earlier it is called only once in its lifetime. If you want to trigger it again you have to shift the control to an entirely new screen and a new state.