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
21 stars 14 forks source link

Starter Code in Algolia's Getting started with Flutter Helper Documentation throws an exception #114

Closed AfiqMutaz closed 7 months ago

AfiqMutaz commented 7 months ago

Describe the bug 🐛
I was just playing around with the Getting Started with Flutter Helper Documentation, and when I started to use my own index it's been giving me Response ({"status":422,"message":"No events to process"}). What I understood was that there is something wrong with the response. I kept trying to backtrack doing the steps in the documentation over and over again to find exactly what this exception means. The first issue I have is that after following section 1 and 2, if you type a couple of characters in the searchbar and when the hits reaches 0 it throws an exception with the same 422 error. My guess is it doesn't know how to handle when there is no data in the response?

At the bottom of the documentation is a final version of the code, copied and pasted that to try to see if the same thing happens and it does, also I had to remove filtering as Facet type is not found or defined. This is not the main issue that I have but I feel like it could be related to the issue I was having when I replaced the mock data with my own index as it was giving the same response error.

To Reproduce 🔍
https://www.algolia.com/doc/guides/building-search-ui/getting-started/flutter/

The exact code that you'd get after following the documentation from 1 to 2

import 'package:algolia_helper_flutter/algolia_helper_flutter.dart';
import 'package:flutter/material.dart';

class SearchMetadata {
  final int nbHits;

  const SearchMetadata(this.nbHits);

  factory SearchMetadata.fromResponse(SearchResponse response) =>
      SearchMetadata(response.nbHits);
}

class TestSearchPage extends StatefulWidget {
  const TestSearchPage({super.key, required this.title});

  final String title;

  @override
  State<TestSearchPage> createState() => _TestSearchPageState();
}

class _TestSearchPageState extends State<TestSearchPage> {
  final _searchTextController = TextEditingController();

  final _productsSearcher = HitsSearcher(
      applicationID: 'latency',
      apiKey: '927c3fe76d4b52c5a2912973f35a3077',
      indexName: 'STAGING_native_ecom_demo_products');

  Stream<SearchMetadata> get _searchMetadata =>
      _productsSearcher.responses.map(SearchMetadata.fromResponse);

  @override
  void initState() {
    super.initState();
    _searchTextController
        .addListener(() => _productsSearcher.query(_searchTextController.text));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Algolia & Flutter'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            SizedBox(
                height: 44,
                child: TextField(
                  controller: _searchTextController,
                  decoration: const InputDecoration(
                    border: InputBorder.none,
                    hintText: 'Enter a search term',
                    prefixIcon: Icon(Icons.search),
                  ),
                )),
            StreamBuilder<SearchMetadata>(
              stream: _searchMetadata,
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return const SizedBox.shrink();
                }
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('${snapshot.data!.nbHits} hits'),
                );
              },
            )
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _searchTextController.dispose();
    _productsSearcher.dispose();
    super.dispose();
  }
}

Stack

RetryStrategy.execute (c:\Users\...\AppData\Local\Pub\Cache\hosted\pub.dev\algolia_client_core-0.3.0\lib\src\transport\retry_strategy.dart:64)
<asynchronous gap> (Unknown Source:0)
InsightsClient.pushEvents (c:\Users\...\AppData\Local\Pub\Cache\hosted\pub.dev\algolia_client_insights-0.3.0\lib\src\api\insights_client.dart:170)
<asynchronous gap> (Unknown Source:0)
AlgoliaEventService.send.<anonymous closure> (c:\Users\...\AppData\Local\Pub\Cache\hosted\pub.dev\algolia_insights-0.2.2\lib\src\algolia_event_service.dart:0)
<asynchronous gap> (Unknown Source:0)

Environment: