- A search box to type your
- Statistics about the current search
- A list to display search results with infinite scrolling
- A refinement list to let users results
Prepare your project
Before you can use Algolia, you need an Algolia account. You can create a new one for free or use the following credentials for an example products dataset:- :
latency - Search :
927c3fe76d4b52c5a2912973f35a3077 - name:
STAGING_native_ecom_demo_products
Create a new Flutter app
In a terminal, run:Add project dependencies
This tutorial uses the Algolia Flutter Helper library to integrate Algolia and the Infinite Scroll Pagination library for infinite scrolling. Addalgolia_helper_flutter and infinite_scroll_pagination as dependencies to your project:
pubspec.yaml
Create a basic search interface
Build a basic search interface with a search box and a search metadata panel for showing the number of search results.Import the Flutter Helper library
Open the file
./lib/main.dart and look for the _MyHomePageState class.
Remove its sample variables and method declarations (_counter, _incrementCounter),
then import the Flutter Helper library:Dart
Add a product searcher
Add the
_productsSearcher property of the HitsSearcher type with your Algolia credentials as parameters.
The HitsSearcher component performs search requests and obtains search results.Dart
Add a search box listener
Add the
_searchTextController property to _MyHomePageState.
It controls and listens to the state of the TextField component you use as the search box.Dart
Extract the number of search results
Add a
SearchMetadata class with the metadata of the latest search.
In this example, it only contains the nbHits value, which is the number of search results.
The SearchMetadata class also has a fromResponse factory method which extracts the nbHits value from the SearchResponse.Dart
Map responses to metadata stream
Add the
_searchMetadata stream which listens to _productSearcher responses and transforms them to SearchMetaData instance.Dart
Create the main UI layout
Override the
build method containing the user interface declaration.
The interface is based on the Scaffold component.
Add the AppBar with “Algolia and Flutter” as its title,
and the Column component as its body:Dart
The
Column’s body consists of three children:
the search box, the metadata panel and the hits list.Add the search input field
Dart
main.dart file,
then build and run your application by running flutter run in a terminal or your development tool.In the simulator, you should see the app bar with title and the search box below.Display the search metadata
Add a
Text widget embedded in Padding and StreamBuilder widgets to show the search metadata.
The StreamBuilder widget ensures update of the Text on each _searchMetadata stream change.Dart
Add search metadata to the UI
Add Build and run the app.
You might see the centered text with the hits count below the search box.
If you type into the search box, the displayed value remains unchanged.
This happens because the
StreamBuilder as the second child to the main Column widget.Dart
_searchTextController and _productsSearcher aren’t connected.To fix it, override the initState method of the _MyHomePageState class and add a listener to the _searchTextController that propagates the input text to the _productsSearcher.Dart
Results list and infinite scrolling
To show search results and their number with infinite scrolling:Add a product class
Add a
Product class that represents a search hit.
To keep this example simple,
it contains a name and an image URL field. Declare a fromJson constructor method for creating Product from a JSON string.Dart
Add a paging controller
Add the
_pagingController component that handles the infinite scrolling logic as _MyHomePageState class property.Dart
Create a hits page class
To declare the
HitsPage class, which represents a page of search results, call the fromResponse factory method which builds a HitsPage from a SearchResponse.Dart
Display hits with infinite scroll
-
Add the
_searchPagestream which listens to_productSearcherresponses and transforms it toHitsPageobject.Dart -
Add the
_hitsfunction to the_MyHomePageStateclass which builds the list of search results. It returnsPagedListView, a component of theinfinite_scroll_paginationlibrary, taking_pagingControlleras parameter.Dart -
Add the results of the
_hitsfunction as the third child to the mainColumnwidget embedded in theExpandedwidget so that it can fill the available screen space.Dart
Connect search page to paging controller
Build and run the app.
You can now see the loading indicator instead of search results.
This happens because the

_pagingController and the _productsSearcher aren’t connected.
To update the _pagingController whenever a new results page is fetched,
add a listener to __searchPage in the initState method.
Add a call to _pagingController.refresh() to the _searchTextController listener callback.Dart
Load next page of results
Build and run the app.
Now it displays the list of search results.
Scroll to the bottom.
Instead of the next results page the loading indicator appears.
Although

_pagingController triggered a request for next page, this request wasn’t processed.
To fix it, complete the initState method by adding a page request listener to _pagingController.
It triggers the loading of the next page in the _productSearcher.Dart
Implement results filtering
To add an extra screen to implement filtering of the search results:Add filter state management
Enable search results filtering by adding a
FilterState property to _MyHomePageState.
FilterState is a component that stores the state of applied filters and provides an interface to alter the state.Dart
Configure a facet list
Add the
FacetList property which manages the appearance of the list of refinement facets for a designated attribute.
Here, the brand attribute is used.Dart
Enable filtering behavior
-
Add the
_filtersmethod to present the filtering interface as a list ofCheckboxListTilesembedded in theScaffoldwidget. TheFacetListclass provides afacetsstream, combining the facets themselves and their selection state as well as atogglemethod that allows to change this state.Dart -
To present the filters screen, add a
GlobalKeyproperty to the_MyHomePageStateclass.Dart- Assign this key to the key property of
Scaffoldin its constructor. - Add
IconButtonto theactionslist of theAppBar. This triggers opening the end drawer. - Assign the
endDrawerproperty ofScaffoldwith filters the widget embedded in theDrawerwidget.
Dart - Assign this key to the key property of
Facet display
Build and run the app.
The app bar now displays the filters button which shows the list of facet values (individual brands) for the brand attribute.

A selection of these values doesn’t affect the search results.
To fix it, connect


FilterState to HitsState in the initState method,
so that each change of FilterState triggers a new search request.
Also, each filter state change might refresh the _pagingController to remove the obsolete loaded pages.
Add the corresponding listener to the _filterState.filters stream.Dart
The final result
Find the source code in the Algolia Flutter playground repository on GitHub. The final version of themain.dart file should look as follows:
Dart
Next steps
This examples shows how to bridge native search with the Algolia Flutter Helper library. You can use it as a basis for more complex applications.- To explore the components in more detail, see the API reference.
- To explore a complete ecommerce app built with Flutter, see the Flutter Ecommerce UI Template.

