Changing and accessing global var in flutter/dart - flutter

I am very new to Dart/Flutter, but I am trying to learn and understand. Here is my issue:
I got global.dart that stores global vars and I know how to access and to change them, but I want a separate class to update itself when a global var is changed from a different class. These classes are stored in different files.
I am not sure if any code is needed in this question..

Based on your requirement, what you need is called ChangeNotifier and you can use a Provider to provide the variables of the ChangeNotifier in any file they are required.
Before using the ChangeNotifiers (can be multiple, like NewsProvider, FeedProvider, CommentProvider etc), you've to inject it into the widget cycle before the Widget/Component you intend to use it in. For that, check the widget Provider or MultiProvider in the Provider linked above.
After setting up the ChangeNotifiers and injecting them into the widget cycle, you can use them in any widget or class as:
For one-time reading the value, use read as:
SampleNotifier sample = context.read<SampleNotifier>();
print(sample.variableName);
Also, in onClick triggered functions, you can't use watch, only read is allowed, as these functions are static in nature and doesn't require dynamically updated value. Also, when you want to access and call a function declared in a ChangeNotifier, for example the setSignIn mentioned below, use read.
For listening to the changes in the variables of the provider, use watch as:
SampleNotifier sample = context.watch<SampleNotifier>();
print(sample.variableName);
Above mentioned read and watch are not the actual functions provided by the package Provider but their short form as extension function on context and you don't need both, this one's enough and better.
A sample ChangeNotifer:
class SampleNotifier extends ChangeNotifier {
//A locally-scoped sample variable
bool _isUserLoggedIn = false;
//Getter to get the sample value
bool get isUserLoggedIn => _isUserLoggedIn;
//Contructor sample to initialize the variables with the default values
//You can initialize it with the global values in your global vars file
FabNotifier() {
_isUserLoggedIn = false;
}
//Sample function to perform some logic and set the updated value
setSignIn() {
_isUserLoggedIn = true;
//To notify all the classes listening to this variable, to update the usage there
//watch is used to listen to the value, so wherever watch is used, it will update the value
notifyListeners();
}
}
To inject this SampleNotifier, check the Provider/MultiProvider in the linked Provide package above and to use it, check the read or watch mentioned above.

Related

Riverpod Flutter: Multiple providers for one screen

I'm new to riverpod, and I want to check that I am doing things correct.
I have a screen on my Flutter app where the use inputs lots of information about a session. Like start time, end time, notes, duration, date etc etc. All this data in the end is stored in a dart complex object MySession(), will all the above properties.
My question is, in the meantime, I am creating a seerate provider for each field. Like this:
final selectedDateProvider = StateProvider((ref) => DateTime.now());
final sessionDurationMinutesProvider = StateProvider<int>((ref) => 0);
Now in the class, I call the providers like this in the build method:
selectedDate = ref.watch(selectedDateProvider);
sessionDurationMinutes = ref.watch(sessionDurationMinutesProvider);
Then I display them in the widgets.
When they are editing, I save the state like this:
ref.read(selectedDateProvider.notifier).state = datePick;
My question is, I have lots and lots of fields on this page. So I have to create lots of providers for each field. Is this the correct practise? Can I not make a customclass for all these fields, and then make one provider which will return this custom class?
On the riverpod docs it says: You should not use StateProvider if: your state is a complex object (such as a custom class, a list/map, ...)
https://riverpod.dev/docs/providers/state_provider
I hope its clear!
Thanks
You already answered your question ;)
Yes, you can.
Make a class that will store the state of all the input fields and expose it through StateProvider.
To do it effectively you will probably need a copyWith method which can be written manually or generated. One of the possible approaches is to use freezed.

Forcing the MobX state to be changed through declared actions only in the Flutter framework

I'm experimenting with MobX state management solution in Flutter. I really like its conciseness and its simplicity but I'm a bit concerned in the drawbacks its way of mutating the state can introduce.
Theoretically , we can force the state to be mutated only using actions and this is great because we can have a history of the changes using the spy feature the package provides.
However, actions are automatically created by the framework if we do not use a specific one. For example, in the code below, I created a counter store class and I used it in the main function to assign a new value for the counter without using a predeclared action. The stric-mode has been previously set to "always".
abstract class _Counter with Store {
#observable
int value = 0;
#action
void increment() {
value++;
}
}
final counter = Counter(); // Instantiate the store
void main() {
mainContext.config = mainContext.config
.clone(isSpyEnabled: true, writePolicy: ReactiveWritePolicy.always);
mainContext.spy((event) {
print("event name : " + event.name);
print("event type : " + event.type);
});
Counter counter = Counter();
counter.value = 10;
}
I was expecting an error to raise saying that it is not possible to change the state outside of action but this does not happen because an ad hoc action is created , called value_set, automatically. I'm wondering ,then, which is the point of forcing to use actions if it is possible to avoid using them. I mean, if we directly change the state, an action is created automatically and seen by the spy functionality making all the process more robust and predictable but, what if I want the state to be changed only by actions I previously implemented? Is there a way of doing so? For example, in the counter code I just provided , is there a way of making the counter incrementalbe only by 1? Because, in the way Mobx works, I could write everywhere in the code something like
counter.value = 10
and this will work just fine without any problems. Forcing people to use preset actions could increase the predicatability a lot and facilitate the teamwork too I think.
I tried to make the value variable private but it still remains accessible from the outside , also if annotated with #readonly.
Hm, that looks weird for me too, because it does work differently in MobX JS (exactly like you describing), but it seems that Dart MobX changed behaviour for single field values and they are automatically wrapped in actions now, yes.
Maybe there should be a optional rule to turn strict checking on again, it would make sense. I suggest you to create and issue or discussion on Dart MobX Github.
More info there: https://github.com/mobxjs/mobx.dart/issues/206
At the end, I found out that the behaviour I was looking for was obtainable with the #readonly annotation. Marking a variable with #readonly annotation avoids automatically creating its setter and getter methods. Moreover it requires the annotated variable to be private, denying the variable to be set directly.
Note: By the way, the way the Dart language is implemented, it is necessary to locate the Counter in a separate file to provide the desired behaviour, otherwise its private fields would be accessible anyway.

Flutter BLoC variables best practice

Started recently using the BLoC approach for building apps, and one thing that is not clear is where to "keep" BLoC variables. I guess we can have these two options:
Declare a variable in the BLoC class; for example in my class I can do the following:
class ModulesBloc extends Bloc<ModulesEvent, ModulesState> {
late String myString;
}
And access it in my UI as follows:
BlocProvider.of<ModulesBloc>(context).myString;
Keep it as a state variable; for example I can declare my state class as follows:
class ModulesState extends Equatable {
const ModulesState({required this.myString});
final String myString;
#override
List<Object> get props => [myString];
}
And access it in my UI as follows:
BlocBuilder<ModulesBloc, ModulesState>(
builder: (BuildContext context, ModulesState modulesState) {
modulesState.myString;
}
)
Are there any performance penalties / state stability issues with any of the above approaches?
Thanks!
I am not sure there is an absolute answer but I can at least give my opinion.
In bloc you have 3 objects: bloc, event, state.
The state is the mutable part while the bloc is a description of the your problem (what states to emit for each event). As such, an immutable variable to describe your problem should be, in my opinion, placed inside the bloc. However, anything which might change is the state of your bloc (same as the state of your widget) and should as such be stored in the state.
Example:
You want to create an app where you can set timers. In this app you can have multiple timers, each of which will be identified by a name.
In this case:
your state will be an object containing a double variable called timeCount, which will be incremented each seconds for example.
You bloc will have a final field called name which will have to be set during the creation of the stopwatch.
Interestingly enough, if you want the bloc to also handle the stopwatch creation, you will have 2 states: the first empty, the second with a name and timeCount. See how naturally name became variable and is therefore found in the state now.

Provider, what's the difference between assigning it to a variable and using it directly?

I have multiple extracted widgets that need to access the provider that is already declared in their parent widget.
I'm trying to access the provider directly using this Provider.of<MyProvider>(context).getSomething(), and it doesn't work. But when I assign it to a variable on top of the build method var provider = Provider.of<MyProvider>(context) then use the method like this provider.getSomething(), it works, why is that?
Will there be a problem if I put this var provider = Provider.of<MyProvider>(context) on every build method of my extracted widgets? or should I just add the provider as a constructor so the parent widget will just pass the provider for every extracted child widget?
In the first one you are only declaring it with that type, as a result the value will be nothing and nothing is assigned to what you declared and so the object is empty and do not have access to the functions or so on. In the second case you are assigning to that object so it will have access to all functions and so on.
no it's ok and it's a common pattern to use.

How do I use ChangeNotifier?

From the docs I understood that one can call addListener() on a ChangeNotifier instance to add a custom listener to the stack.
This method accepts a callback with zero arguments (according to notifyListeners()), e.g.:
class MyClass extends ChangeNotifier {
MyClass() {
addListener(() {
// ...
});
}
}
From within the callback, how does one find out what properties or parts of MyClass have been changed?
ChangeNotifier does not have such capabilities inherently. You will have to implement your own logic. Specifically, you either have access to all of the properties of your ChangeNotifier implementation because you add the listener in its scope or you have access to it because you have a reference to it in your scope.
ChangeNotifier simply implements Listenable and provides some utilities for managing listeners. Furthermore, the documentation states the following about it:
ChangeNotifier is optimized for small numbers (one or two) of listeners. It is O(N) for adding and removing listeners and O(N²) for dispatching notifications (where N is the number of listeners).
I am not sure about options with better runtime complexity for notifying listeners, but you will not run into any issues in a regular Flutter app.
ValueNotifier
ValueNotifier is a pre-made implementation of ChangeNotifier that will notify its listeners when its value property is changed.
This is sufficient for most case, but since it appears that you want to create a custom ChangeNotifier, you can use the source code of ValueNotifier to take a look at an example implementation (it is very straight forward).
If you are just looking to do state management in general, ValueNotifiers usually work great. However, they are not applicable in every scenario. Hence, here is an extensive list with different state management options.
Considering the questions, I think the techniques that fit your needs best and the most popular options are the following:
InheritedWidget as it lets you notify dependents based on what data changed. Additionally, there is InheritedModel as an extension of this and InheritedNotifier that works with Listenable, just like ChangeNotifier does.
The BLOC pattern, which works with streams.
The provider package which is mostly a convenience wrapper for various Flutter state management techniques (InheritedWidget, StatefulWidget, ValueNotifier, etc.).