2

I am developing mobile application with flutter using flutter hooks. I want to refresh Riverpod FuttureProvider using useEffect, but it doesn't work nicely...

My code is

@hwidget
Widget someWidget(BuildContext context) {
  final futureProvider = useProvider(myFutureProvider);

  useEffect(() {
    context.refresh(futureProvider);
  }, [someKeys]);

  return searchFetch.when(
      data: (data) => TextButton(
          onPressed: () {
              context.refresh(futureProvider);
          },
          child: Text('refresh')
      ),
      loading: () => Text('Loading...'),
      error: (error, stack) => Text('Error.'));
}

When tap TextButton, refresh works fine.(I can see 'Loading...' text.) However, refresh written by useEffect only updates the data and 'Loading...' text cannot be seen.

How should I rewrite it.

  • You are not refreshing the same providers, in the button you refresh `searchFetchProvider`, and in the useEffect `futureProvider` – croxx5f Sep 11 '21 at 21:22
  • I think on `futureProvider` you need refresh by `context.read(myFutureProvider).initData()` and it should reflect the UI, and i prefer using `Consumer` . – Md. Yeasin Sheikh Sep 12 '21 at 05:04
  • @croxx5f Sorry, that was a typo when abstracting my code. In my production code, they are the same Provider. The code I wrote for the question has been fixed. – Kataoka Hiroki Sep 12 '21 at 11:27
  • @YeasinSheikh It looks like there is no initData() in context.read(FutureProvider) – Kataoka Hiroki Sep 12 '21 at 18:50
  • You should update both hooks and riverpod packages to the latest version. You can no longer do context.read and useProvider has been deprecated. – Agon Noga Sep 12 '21 at 22:44

0 Answers0