The screen is refreshes twice when I use this state notifier provider, how can I prevent it? I want to load the api data once and only refresh if firestore document is updated.
final profileAsyncController =
StateNotifierProvider<ProfileAsyncNotifier, AsyncValue<ProfileUser?>>(
(ref) => ProfileAsyncNotifier(ref));
class ProfileAsyncNotifier extends StateNotifier<AsyncValue<ProfileUser?>> {
ProfileAsyncNotifier(this.ref) : super(AsyncLoading()) {
_init();
}
final Ref ref;
void _init() async {
var profile = await ref.read(apiServiceProvider)!.getProfile();
// if (!mounted) return;
state = AsyncData(profile);
final firestoreUser = ref.watch(firestoreUserChangeProvider);
final firestoreDb = ref.read(firestoreDatabaseProvider);
firestoreUser.whenData((value) {
if (value!.syncRequired!) {
firestoreDb!.stopSync();
}
});
}
void updateProfile(ProfileUser? profile) async {
state = AsyncLoading();
state = AsyncData(profile);
}
}
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
body: Consumer(builder: (context, watch, child) {
var profile = ref.watch(profileAsyncController);
return profile.when(
loading: () => Container(child: Scaffold(body: Center(child: const CircularProgressIndicator()))),
error: (err, stack) => Text('Error $err'),
data: (profile) => profileWidget(user: profile),
);
}),
);
}