File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
templates/dart/guides/search Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ import 'package:http/http.dart' as http;
2+ import 'dart:convert';
3+ { {> snippets/import} }
4+
5+ void main() async {
6+ // initiate client
7+ {{> snippets/init} }
8+
9+ // read json file from url
10+ final datasetRequest = await http.get(
11+ Uri.parse('https://dashboard.algolia.com/sample_datasets/movie.json'));
12+
13+ if (datasetRequest.statusCode == 200) {
14+ final moviesData = jsonDecode(datasetRequest.body);
15+
16+ final batchRequests = < BatchRequest> [];
17+
18+ for (final movie in moviesData) {
19+ batchRequests.add(
20+ BatchRequest(action: Action.fromJson(' addObject' ), body: movie),
21+ );
22+ }
23+
24+ // testing algolia method snippet
25+ { {#dynamicSnippet} }saveObjectsMovies{ {/dynamicSnippet} }
26+
27+ try {
28+ // push data to algolia
29+ await client.batch(
30+ indexName: ' movies_index' ,
31+ batchWriteParams: BatchWriteParams(requests: batchRequests),
32+ );
33+ print(" Successfully indexed records!" );
34+ } catch (e) {
35+ print(" Error: ${e.toString()}" );
36+ }
37+ } else {
38+ throw Exception(' Failed to load data' );
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments