0

With this below code i'm trying to get data from server when Flutter lifecycle resumed, but it doesn't work for me and i'm not sure how can i resolve that

class Home extends HookConsumerWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final scaffoldKey = useMemoized(() => GlobalKey<ScaffoldState>());
    final appLifecycleState = useAppLifecycleState();
    final showLoading = useState(false);

    useEffect(() {
      ref.read(bakersProvider.notifier).send(
        method: HTTP.GET,
        endPoint: Server.$onlineBakersList,
        parameters: {},
      );
      if (appLifecycleState == AppLifecycleState.resumed) {
        debugPrint('resumed');
        ref.read(bakersProvider.notifier).send(
          method: HTTP.GET,
          endPoint: Server.$onlineBakersList,
          parameters: {},
        );
      }

      return (){};
    }, [appLifecycleState]);
DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • Have you have added WidgetsBindingObserver mixin and listening to the AppLifeCycle changes inside didChangeAppLifecycleState method – Sanketh B. K Sep 24 '22 at 05:37
  • @SankethB.K i used `HookConsumerWidget` and i think it doesn't necessary – DolDurma Sep 24 '22 at 05:46
  • HookConsumerWidget is related to riverpod, but it doesn't help to listen to changes in lifecycle methods, you can cross check by printing the lifecycle method if it is changing. Refer to this https://stackoverflow.com/questions/50131598/how-to-handle-app-lifecycle-with-flutter-on-android-and-ios – Sanketh B. K Sep 24 '22 at 05:50
  • I tried your code and it worked fine...What version of `hooks_riverpod` are you using? – Josteve Sep 25 '22 at 14:25
  • @Josteve `riverpod: ^1.0.3 - flutter_hooks: ^0.18.5+1 - hooks_riverpod: ^1.0.4`, could you get data from server on `lifecycle.resumed` ? – DolDurma Sep 25 '22 at 15:24

1 Answers1

2

this issue solved. reference

useOnAppLifecycleStateChange((pref, state) {
  if (state == AppLifecycleState.resumed) {
    //make a request
  }
});
DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • Hey, I can't even use 'HookConsumerWidget', how can I get that? I tried to add flutter_hook, riverpod_hook, and more. It does not pop out on my Flutter :( – chichi Apr 26 '23 at 08:15