0

When using Flutter and Riverpod, how do I update its values from my business logic?

I understand that I can get and set values from the UI side.

class XxxNotifier extends StateNotifier<String> {
  XxxNotifier() : super("");
}

final xxxProvider = StateNotifierProvider<XxxNotifier, int>((ref) {
  return XxxNotifier();
});

class MyApp extends HookConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // getValue
    final String value = ref.watch(xxxProvider);

    // setValue
    context.read(xxxProvider).state = "val";

    return Container();
  }
}

This method requires a context or ref.

How do I get or set these states from the business logic side?

Passing a context or ref from the UI side to the business logic side might do that, but I saw no point in separating the UI and business logic. Perhaps another method exists.

Perhaps I am mistaken about something. You can point it out to me.

Ganessa
  • 782
  • 2
  • 7
  • 24
  • what you are doing is not a business logic. its a UI logic and there is no point on using StateNotifier for storing only string. Use stateProvider instead. – john Oct 24 '22 at 08:49
  • We cannot share the real code due to NDA. This is just a sample. It is actually status of various types. – Ganessa Oct 24 '22 at 11:17

2 Answers2

1

You can pass ref in your XxxNotifier class:

class XxxNotifier extends StateNotifier<String> {
  XxxNotifier(this._ref) : super("");

  final Ref _ref;

  void setNewState() {
    state = 'to setting';
    // use `_ref.read` to read state other provider
  }
}

final xxxProvider = StateNotifierProvider<XxxNotifier, int>((ref) {
  return XxxNotifier(ref);
});

// or using tear-off
final xxxProvider = StateNotifierProvider<XxxNotifier, int>(XxxNotifier.new);
Ruble
  • 2,589
  • 3
  • 6
  • 29
  • Yes, I do. We know we can do that by passing ref. We want to know if that is the right way to use it or if there is a better way. – Ganessa Oct 24 '22 at 11:19
  • Basically, we can't get the state of the provider without using `ref`. This method is recommended by the author of the package. – Ruble Oct 24 '22 at 11:35
  • I see. Then we can pass ref to StateNotifier and use it. – Ganessa Oct 24 '22 at 13:10
0

You can create methods in your XxxNotifier class to modify the state of your provider.

For example, your notifier class can look like this.

class TodosNotifier extends StateNotifier <List<Todo>> {
  TodosNotifier(): super([]);

  void addTodo(Todo todo) {
    state = [...state, todo];
  }

}

You can then read the provider in a callback.

ref.read(xxxProvider.notifier).addTodo(todo);
Tayormi
  • 1,150
  • 8
  • 12
  • Thanks for your answer, it is very helpful. But we wanted to know about how to refer to it from business logic . Do we still have to pass the ref? – Ganessa Oct 24 '22 at 11:21
  • 1
    You can pass the ref as it is done in the docs: https://docs-v2.riverpod.dev/docs/concepts/reading#obtaining-a-ref-from-a-provider – Tayormi Oct 24 '22 at 15:31
  • Thank you. That's the correct way to implement it since it's written in the example in the documentation. – Ganessa Oct 25 '22 at 06:54