Skip to content

Commit 79cdf48

Browse files
committed
add dart onboarding guide
1 parent 2a142c8 commit 79cdf48

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)