Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement global states #213

Open
LeadcodeDev opened this issue Oct 28, 2024 · 0 comments · Fixed by #217
Open

Implement global states #213

LeadcodeDev opened this issue Oct 28, 2024 · 0 comments · Fixed by #217
Assignees

Comments

@LeadcodeDev
Copy link
Member

Context

When you develop your entire application in a single file, each of your variables is accessible from any consumer located in your code.

In the context of a growing application, it's a good idea to divide it up into several classes with a single responsibility, but we quickly find ourselves with a multitude of files and increasing difficulty in sharing our data.

The aim of Shared States is to simplify this sharing across your entire application.

Proposal

This contract requires us to define an initial state for our shared data, which may subsequently be altered.

abstract interface class GlobalState<T> {
  T state;
}

We implement the contract while defining our generic T and its state of the same type.

final class MyState implements GlobalState<int> {
  @override
  int state = 0;

  void increment() => state++;
  void decrement() => state--;
}

final class MyClass with InjectState {
  final myState = state.read<MyState>();
  myState.increment();

  print(myState.state); // print(1);
}
@LeadcodeDev LeadcodeDev self-assigned this Oct 28, 2024
@LeadcodeDev LeadcodeDev converted this from a draft issue Oct 28, 2024
@LeadcodeDev LeadcodeDev changed the title Implémentation global states Implement global states Oct 29, 2024
@LeadcodeDev LeadcodeDev linked a pull request Nov 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Work In Progress
1 participant