-2

Custom backend state management question. I think this is a little subjective but I do want an objective side to this such as, why coud this be bad, or good and can this be simpler and better than any other state management option such as riverpod, at least for a small part of my backend.

I'm designing a state management for my backend and want to avoid the verboseness of Providers and the instantiations also. My front end code utilizes Riverpod beautifully, this is not a question referring to front end.

How does this state management look for backend code? Will I encounter any kinds of issues?:

void main() {
  
  // Set name
  DatabaseRepository.createName('John');
  print(State.getName());
  
  // Update database & state, name
  DatabaseRepository.createName('Terry');
  print(State.getName());
  
  }
  
abstract class State {
  static String? _name;
  
  static void getNameFromDatabase(String name) {
    _name = name;
  }
  
  static String getName() => _name!;
}

abstract class DatabaseRepository {
  static String? _name;
  static void createName(String name) {
    _name = name;
    State.getNameFromDatabase(_name!);
  }
}
RobbB
  • 1,214
  • 11
  • 39

1 Answers1

1

There is no state for backend. In the backend, all that matters that the code is executed in the right order.

If there is nothing to update, what is the point of having a state to begin with?

If there is something to update, where in your code are you updating it?

Aldrin Mathew
  • 146
  • 1
  • 1
  • 6
  • This is a good point. So I need to get updates and they get passed around the backend… this is how I’m doing this- how I’m getting the updates (which is the state no?). Also my app is pretty big, so I need to separate concerns more than usual – RobbB Mar 29 '22 at 22:38
  • 1
    @RobbB Checkout [Turbo](https://pub.dev/packages/turbo) since I think that suits your style more. I am the creator of the package by the way. Also, instead of functions to get, update and change values, try to use getters and setters more. On thing about **Turbo** to keep in mind is that you have to call `refresh()` to update widgets and states – Aldrin Mathew Mar 30 '22 at 07:14
  • Thank you for the recommendation! Looks like a very nice package, I like how simple it is. I might just give it a go – RobbB Mar 30 '22 at 13:24