Flutter SDK
Complete guide to using Cocobase with Flutter and Dart applications.Installation
Add Cocobase to yourpubspec.yaml:
Quick Start
Initialize Cocobase
Basic Operations
Querying Data
CocoBase Flutter provides two powerful ways to query your data: Filter Map (simple) and QueryBuilder (advanced).Method 1: Filter Map (Recommended for Simple Queries)
Pass filters as a simpleMap<String, dynamic>:
- ✅ Simple and readable
- ✅ No need to learn QueryBuilder syntax
- ✅ Works with all operators
- ✅ Perfect for beginners
Method 2: QueryBuilder (For Complex Queries)
For more complex queries, use the fluent QueryBuilder:- 🔗 Chainable methods - Build queries step by step
- 📝 Self-documenting - Method names are clear (
whereGreaterThan, not__gt) - 🔍 IDE support - Get autocomplete and type hints
- 🎯 Complex queries - Combine multiple conditions easily
Operators Reference
Comparison Operators
| Operator | Filter Map | QueryBuilder Method | Example |
|---|---|---|---|
| Equal | field: value | .where(field, value) | 'age': 25 |
| Greater Than | field__gt | .whereGreaterThan(field, value) | 'age__gt': 18 |
| Greater or Equal | field__gte | .whereGreaterThanOrEqual(field, value) | 'age__gte': 18 |
| Less Than | field__lt | .whereLessThan(field, value) | 'age__lt': 65 |
| Less or Equal | field__lte | .whereLessThanOrEqual(field, value) | 'age__lte': 65 |
| Not Equal | field__ne | .whereNotEqual(field, value) | 'status__ne': 'deleted' |
String Operators
| Operator | Filter Map | QueryBuilder Method | Example |
|---|---|---|---|
| Contains | field__contains | .whereContains(field, value) | 'title__contains': 'flutter' |
| Starts With | field__startswith | .whereStartsWith(field, value) | 'email__startswith': 'admin' |
| Ends With | field__endswith | .whereEndsWith(field, value) | 'domain__endswith': '.com' |
Array/List Operators
| Operator | Filter Map | QueryBuilder Method | Example |
|---|---|---|---|
| In Array | field__in | .whereIn(field, values) | 'status__in': 'active,pending' |
| Not In Array | field__notin | .whereNotIn(field, values) | 'status__notin': 'deleted,archived' |
Special Operators
| Operator | Filter Map | QueryBuilder Method | Example |
|---|---|---|---|
| Is Null | field__isnull | .whereIsNull(field, bool) | 'deletedAt__isnull': true |
OR Queries
CocoBase supports three types of OR queries for different needs.Type 1: Simple OR Conditions
Use when you want “field1 = value1 OR field2 = value2”:Type 2: Multi-Field Search
Use when you want to search across multiple fields:Type 3: Named OR Groups
Use when you want to group OR conditions:Sorting and Pagination
Sorting
Using QueryBuilder:Pagination
Using QueryBuilder:Field Selection and Population
Select Specific Fields
Include only certain fields in the response:Population (Relationships)
Load related documents automatically:Type Conversion & Type Safety
Learn how to work with strongly-typed documents and eliminate null safety issues.Why Type Safety?
Without Type Safety (Dynamic):Creating Models
Basic Model:Registration Methods
Method 1: Global Registration (Recommended) Register converters once in your app initialization:- ✅ Register once, use everywhere
- ✅ Cleaner API calls
- ✅ Best for production code
Using Converted Documents
List of Typed Documents:Creating Documents with Type Safety
When you create new documents, your model class must have atoJson() method:
- You create a
Bookinstance with your data createDocument<Book>()callstoJson()automatically to serialize it- The API receives the JSON and stores it
- The response is converted back to a
Bookinstance usingfromJson() - You get a
Document<Book>with the server-assigned ID
Batch Operations
Advanced Query Examples
Example 1: Search with Filters
Find published books by specific authors:Example 2: Price Range Query
Find products in a price range:Example 3: Complex OR Logic
Find premium users (verified OR have payment method):Example 4: Using Filter Map for Complex Query
Authentication
Email/Password
register() and login() methods return null. After successful authentication, use getUser() to retrieve the authenticated user details.
Complete Authentication Example
OAuth
Real-time Data
Watch Collection
Watch Document
Building Reusable Queries
Create query builders as functions:Debugging Queries
Print the Query String
Best Practices
1. Always Define fromJson() and toJson()
2. Handle Optional Fields
3. Use Type Casting in fromJson()
4. Register Converters Early
5. Use Filter Map for Simple Queries
6. Always Limit Results
7. Error Handling
Troubleshooting
Issue: Type mismatch error
Cause: Field type mismatch infromJson()
Issue: Converter not registered
Cause: Forgot to register converterIssue: Null safety error
Cause: Optional field treated as requiredQuery Limits
- Maximum limit: 1000 documents per request
- Offset range: 0 to 100,000
- Field name length: 255 characters
- Filter value length: 10,000 characters
Next Steps
- JavaScript SDK - Learn about the JS/TS SDK
- Authentication - Deep dive into auth
- Querying - Advanced query techniques
- Real-time - Real-time data synchronization
