-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_case_2_api_query.dart
More file actions
51 lines (41 loc) · 1.36 KB
/
use_case_2_api_query.dart
File metadata and controls
51 lines (41 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Use Case 2 from README: Fetch API Data & Query Instantly
// This matches the example shown in the "The Magic" section
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:tiny_db/tiny_db.dart';
Future<void> main() async {
final db = TinyDb(MemoryStorage());
try {
print('Fetching from GitHub API...\n');
// Fetch JSON from any API
final response = await http.get(
Uri.parse('https://api.github.com/users/vento007/repos'),
);
if (response.statusCode != 200) {
print('Failed to fetch: ${response.statusCode}');
return;
}
final repos = jsonDecode(response.body) as List;
print('Fetched ${repos.length} repositories\n');
// Make it queryable
for (final repo in repos) {
await db.insert(repo);
}
// Query nested data, complex conditions - instantly
final dartPackages = await db.defaultTable.search(
where('language').equals('Dart').and(
where('stargazers_count').greaterThan(3),
),
);
print('Found ${dartPackages.length} popular Dart packages!');
for (final pkg in dartPackages) {
print('${pkg['name']}: ${pkg['stargazers_count']} ⭐');
}
print('\nExpected output:');
print('flexible_tree_layout: 6 ⭐');
print('riverpod_mvcs_example: 5 ⭐');
print('tiny_db: 4 ⭐');
} finally {
await db.close();
}
}