Having Problem Trying To Use and Update Variables Inside Plain Dart Text - Flutter

79 Views Asked by At

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.

1

There are 1 best solutions below

1
coder_ceho54 On

You can use the setState() method to update the state of a widget in Flutter. However, since your Faith class is not a widget, you cannot use setState() directly. Instead, you can use a state management solution like Provider or Riverpod to manage the state of your Faith class.

Here is an example of how you can use Riverpod to manage the state of your Faith class:

import 'package:flutter_riverpod/flutter_riverpod.dart';

final faithProvider = StateNotifierProvider((_) => FaithNotifier());

class FaithNotifier extends StateNotifier<Faith> {
  FaithNotifier() : super(Faith(childsName: "Joe"));

  void updateChildsName(String name) {
    state = state.copyWith(childsName: name);
  }

  void addEvent(Event event) {
    state = state.copyWith(simulation: [...state.simulation, event]);
  }

  void removeEvent(Event event) {
    state = state.copyWith(
        simulation: state.simulation.where((e) => e != event).toList());
  }
}

class Faith {
  final String childsName;
  final List<Event> simulation;

  Faith({required this.childsName, required this.simulation});

  Faith copyWith({String? childsName, List<Event>? simulation}) {
    return Faith(
      childsName: childsName ?? this.childsName,
      simulation: simulation ?? this.simulation,
    );
  }
}

class Event {
  final int eventCode;
  final String eventTitle;
  final String eventImage;
  final List<Choice> choices;

  Event({
    required this.eventCode,
    required this.eventTitle,
    required this.eventImage,
    required this.choices,
  });
}

class Choice {
  final String choiceText;

  Choice({required this.choiceText});
}

In this example, we define a FaithNotifier class that extends StateNotifier<Faith>. The Faith class contains a childsName property and a _simulation property, which is a list of Event objects. The FaithNotifier class provides methods to update the childsName property and add or remove events from the _simulation list.

We then define a faithProvider using StateNotifierProvider, which creates a new instance of FaithNotifier when it is first accessed. We can then use the useProvider hook to access the FaithNotifier instance and update the state of our Faith class.

I hope this helps! Let me know if you have any further questions.