algoliasearch-helper-flutter icon indicating copy to clipboard operation
algoliasearch-helper-flutter copied to clipboard

Data loading does not work.

Open Patrick386 opened this issue 1 year ago • 1 comments

I combined Riverpod to process the screen and data. Currently, the package only supports Stream. I cannot display the Loading state of the data on the screen. Is it possible to support Future? Or is there a way to know the Data Loading state?

Note that I am not using 'StreamBuilder'. Even after changing to 'StreamNotifier', the issue remains the same.

@riverpod
class ItemPagingAlgoliaController extends _$ItemPagingAlgoliaController {
  late final HitsSearcher hitsSearcher;

  @override
  FutureOr<AlgoliaDataState<ItemData>?> build({bool published = true}) {

    state = AsyncValue.data(AlgoliaDataState<ItemData>( hitsPerPage: 20));

    hitsSearcher = HitsSearcher(
      applicationID: Env.algoliaApplicationID,
      apiKey: Env.algoliaSearchOnlyKey,
      indexName: AlgoliaCredentials.itemIndex,
    );

    hitsSearcher.responses.listen(_fetchDataUpdate); // ** Stream listen
    return state.value;
  }

  void _fetchDataUpdate(){
          ...
         update((s)=> s.copyWidth(items: items));
      }
}

UI:

class _DataListScreen extends ConsumerWidget {
  const _DataListScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    var sts = ref.watch(itemPagingAlgoliaControllerProvider(published: true));

    return sts.when(
        data: (dataState) {
          return _DataTableScreen(items: dataState?.items ??[]);
        },
        error: (s, t) => Text('Error'),
        loading: () => const LoadingData()); //*** Loading not working..
  }
}

Patrick386 avatar May 12 '24 08:05 Patrick386

I have tried several tests, but I couldn't find a solution other than using StreamBuilder.

    /// Loading Indicator
                  StreamBuilder(
                      stream: ctr.responseData, // hitsSearcher.responses
                     builder: (context, snapshot) {               
                        if (snapshot.connectionState ==
                            ConnectionState.waiting) {
                          return const LinearProgressIndicator();
                        }
                        return const SizedBox.shrink();
                      }),

Patrick386 avatar May 12 '24 13:05 Patrick386