I am quite new to Flutter & Dart. I have a plain dart class called "Faith" and this class is not wrapped in any widget. Been trying to use variables/states inside choices and event title. How should I be creating, using and updating variables/states inside plain Dart class? Thanks in advance.
class Faith{
String? childsName = "Joe";
List<Events> _simulation= [
Events(
eventCode: 0,
eventTitle:
"Welcome ${childsName}, to the world of darkness.",
eventImage: "assets/images/welcoming.jpg",
choices: [
Choice(choiceText: "Continue"),
Choice(choiceText: "Go Back"),
],
),
];
I have looked for state management tools such as Riverpod and native flutter state management system. However I did not succeed. I failed to use and update variables inside my class.
You can use the
setState()method to update the state of a widget in Flutter. However, since yourFaithclass is not a widget, you cannot usesetState()directly. Instead, you can use a state management solution likeProviderorRiverpodto manage the state of yourFaithclass.Here is an example of how you can use
Riverpodto manage the state of yourFaithclass:In this example, we define a
FaithNotifierclass that extendsStateNotifier<Faith>. TheFaithclass contains achildsNameproperty and a_simulationproperty, which is a list ofEventobjects. TheFaithNotifierclass provides methods to update thechildsNameproperty and add or remove events from the_simulationlist.We then define a
faithProviderusingStateNotifierProvider, which creates a new instance ofFaithNotifierwhen it is first accessed. We can then use theuseProviderhook to access theFaithNotifierinstance and update the state of ourFaithclass.I hope this helps! Let me know if you have any further questions.