Issue
So, I am in my home_screen dart file and I have a Text in it where I want it to display the live value from another class's properties value. When I update the class's properties value from another script I want my home_screen Text to update to that new value. My class in a separate file is the following (simplified) :
class MyClass {
static int myNumber = 10;
}
I want the following Text to update with the value of myNumber in another file:
Text('iWantThisToDisplayLiveMyNumber'),
When I change the value of myNumber from yet another random script how do I get the Text
to update in live time to always be myNumber?
Solution
Look into Providers.
class MyClass with ChangeNotifier {
static int myNumber = 10;
void changeNumber(){
myNumber = 1;
notifyListeners();
}
}
In your Widget class you can set up a "listener" like this:
int updatedNumber = Provider.of<MyClass>(context, listen: true).myNumber;
This is a basic and bad way of doing this. Please check out the "ChangeNotifier" "ChangeNotifierProvider" and "Provider.of" Sections in the docs here.
https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple
Its pretty easy to get started with. Good luck
Answered By - Benedikt J Schlegel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.