Collections — Flutter SDK
Create a collection client
Section titled “Create a collection client”class Post { final String id; final String title; final String body; Post({required this.id, required this.title, required this.body}); factory Post.fromJson(Map<String, dynamic> j) => Post(id: j['id'], title: j['title'], body: j['body']);}
final posts = bunbase.collection('posts', Post.fromJson);final result = await posts.list(const ListQuery( sort: '-_created_at', page: 1, perPage: 20,));// result.items — List<Post>// result.total — int (total count)// result.page — current pageGet by ID
Section titled “Get by ID”final post = await posts.get('rec_01J...');
// With relation expansion:final post = await posts.get('rec_01J...', expand: ['author']);Create
Section titled “Create”final post = await posts.create({'title': 'Hello', 'body': 'World'});Create many
Section titled “Create many”final created = await posts.createMany([ {'title': 'Post A'}, {'title': 'Post B'},]);Update
Section titled “Update”final updated = await posts.update('rec_01J...', {'title': 'Updated'});Delete
Section titled “Delete”await posts.delete('rec_01J...');Restore (soft-delete)
Section titled “Restore (soft-delete)”final restored = await posts.restore('rec_01J...');final total = await posts.count();
// With filter:final active = await posts.count( filter: {'status': StringFilter('published')},);Filtering
Section titled “Filtering”Simple equality
Section titled “Simple equality”ListQuery(filter: {'status': StringFilter('active')})Typed filter values
Section titled “Typed filter values”| Class | Dart type | Example |
|---|---|---|
StringFilter | String | StringFilter('active') |
IntFilter | int | IntFilter(42) |
DoubleFilter | double | DoubleFilter(3.14) |
BoolFilter | bool | BoolFilter(true) |
StringListFilter | List<String> | StringListFilter(['a','b']) |
IntListFilter | List<int> | IntListFilter([1,2,3]) |
Comparison operators
Section titled “Comparison operators”Use OpFilter with a map of operator → value:
ListQuery(filter: { 'age': OpFilter({ 'gte': IntFilter(18), 'lt': IntFilter(65), }),})Supported operators: eq, neq, gt, gte, lt, lte, in, nin, contains, startsWith.
Full-text search
Section titled “Full-text search”ListQuery(search: 'flutter tutorial')Pagination
Section titled “Pagination”var page = 1;while (true) { final result = await posts.list(ListQuery(page: page, perPage: 50)); process(result.items); if (result.items.length < 50) break; page++;}Realtime channel helpers
Section titled “Realtime channel helpers”Use these to subscribe to changes without hard-coding strings:
bunbase.realtime.subscribe(posts.channel, onEvent: (e) { ... }); // all changesbunbase.realtime.subscribe(posts.mineChannel, onEvent: (e) { ... }); // auth user onlybunbase.realtime.subscribe(posts.recordChannel(id), onEvent: (e) { }); // single recordbunbase.realtime.subscribe(posts.recordsChannel, onEvent: (e) { ... }); // multi-record IDs