You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From what I understand, riverpod's provider maintains objects.
For example, when a user logs in, the ID of the person logging in must be saved and retained so that it can continue to be used on other pages.
However, during development, it was confirmed that the status was not maintained.
To simply test it, I wrote the following code.
Assume that the first startup screen is home3 without using ref.
Go to the home and home2 pages and update the state to raise the number. After popping out, the data is initialized when you switch pages to home or home2 again.
Assume that the first start screen is home using ref.
Go to the home or home2 page, update the state, go to the home3 page, exit with pop, and check the state again in home or home2 to confirm that the state is maintained.
I'm in the process of learning the flutter.
Is it right for me to understand and use it?
Is it correct that there should be no case of initialization like the example number 1?
import 'package:counter/state.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'providers.g.dart';
@riverpod
class Counter extends _$Counter {
/// Classes annotated by `@riverpod` **must** define a [build] function.
/// This function is expected to return the initial state of your shared state.
/// It is totally acceptable for this function to return a [Future] or [Stream] if you need to.
/// You can also freely define parameters on this method.
@override
AuthState build() {
print('tttttttttttttttttt');
return AuthState();}
void increment() {
print(state.num);
int num = state.num+1;
state = state.copyWith(num: num);
}
}
@riverpod
Counter count(CountRef ref) {
final state = ref.watch(counterProvider.notifier);
return state;
}
state.dart
import 'package:freezed_annotation/freezed_annotation.dart';
part 'state.g.dart';
part 'state.freezed.dart';
@freezed
class AuthState with _$AuthState {
const factory AuthState({@Default(0) int num}) = _AuthState;
factory AuthState.fromJson(Map<String, Object?> json) =>
_$AuthStateFromJson(json);
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
From what I understand, riverpod's provider maintains objects.
For example, when a user logs in, the ID of the person logging in must be saved and retained so that it can continue to be used on other pages.
However, during development, it was confirmed that the status was not maintained.
To simply test it, I wrote the following code.
Assume that the first startup screen is home3 without using ref.
Go to the home and home2 pages and update the state to raise the number. After popping out, the data is initialized when you switch pages to home or home2 again.
Assume that the first start screen is home using ref.
Go to the home or home2 page, update the state, go to the home3 page, exit with pop, and check the state again in home or home2 to confirm that the state is maintained.
github repository : https://github.com/kschoi93/riverpod-state-test.git
I'm in the process of learning the flutter.
Is it right for me to understand and use it?
Is it correct that there should be no case of initialization like the example number 1?
code 1
code 2
other code
providers.dart
state.dart
Beta Was this translation helpful? Give feedback.
All reactions