algolia / algoliasearch-helper-flutter

⚡️ Building block to create instant-search applications with Flutter
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/flutter/
Other
22 stars 15 forks source link

Data loading does not work. #135

Open Patrick386 opened 4 months ago

Patrick386 commented 4 months ago

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 commented 4 months ago

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();
                      }),