Official Flutter plugin bmob_plugin(source bmob-flutter-sdk/data_plugin). Data models inherit BmobObject + JSON serialization(similar to Android), queries use generic BmobQuery<T>, return asynchronously Future + .then / .catchError。
The complete API is subject to the BmobDocs Flutter documentation ; the agent can directly WebFetch metadata.docs_raw。
Core principles
1. Installation and reference
flutter pub add bmob_plugin
import 'package:bmob_plugin/bmob_plugin.dart';
2. Initialization — Console → Settings → Application keys → Secret Key、API security code;Do notfill in masterKeyon the client side (the third optional parameter is only for server-side scenarios).
// main() 或 App 启动最早处
Bmob.initialize(secretKey, apiSafe);
// 不推荐:Bmob.initialize(secretKey, apiSafe, masterKey);
3. Switch to a registered domain before launch — resetDomain must be before initialize before(the built-in test domain during development has a request count limit):
Bmob.resetDomain("http://api.yourdomain.com");
Bmob.initialize(secretKey, apiSafe);
4. Custom table models — one Dart class per business table extends BmobObject, and implement fromJson / toJson(see references/model-and-init.md). The table name defaults to the same as the class name (e.g. Blog → table Blog)。
5. Error handling — uniformly use BmobError.convert(e) to get code / error:
}).catchError((e) {
final err = BmobError.convert(e);
print('${err.code}: ${err.error}');
});
6. Reserved fields — objectId、createdAt、updatedAt、ACL are maintained by the SDK / server; business code must set objectId。
Security checklist
- The client must not pass the Master Key to
Bmob.initializethe third parameter. - Secret Key / API security code use
--dart-define、.env+flutter_dotenvor CI injection, do not commit to git. - Before release,
resetDomainis the registered domain, and the order is beforeinitialize. - Tables written to must be configured with ACL(
blog.setAcl(bmobAcl)), otherwise any user can modify any row. - Android file upload / download requires adapting storage permissions first (see the official documentation "File Operations").
- Real-time listener
RealTimeDataManagerin the callbackdata.dataisMap, do not directly use it asBlog.
Common questions
Cross-platform Q&A:shared/faq.md(including Flutter routing instructions).
Anti-patterns
See shared/anti-patterns.md. Key points for this platform: do not pass masterKeyon the client; do not directly treat Map in real-time callbacks as a model.
Single-record CRUD (using Blog as an example)
class Blog extends BmobObject {
String? title;
String? content;
int? like;
BmobUser? author;
Blog();
Blog.fromJson(Map<String, dynamic> json) {
objectId = json['objectId'];
title = json['title'];
content = json['content'];
like = json['like'];
if (json['author'] != null) {
author = BmobUser()..objectId = json['author']['objectId'];
}
}
Map<String, dynamic> toJson() => {
'title': title,
'content': content,
'like': like,
if (author != null) 'author': author,
};
}
Create
final blog = Blog()
..title = '博客标题'
..content = '博客内容'
..like = 77;
blog.save().then((BmobSaved saved) {
print(saved.objectId);
}).catchError((e) => print(BmobError.convert(e).error));
Query a single record (including Pointer include)
final q = BmobQuery<Blog>();
q.setInclude('author');
q.queryObject(objectId).then((data) {
final blog = Blog.fromJson(data);
print(blog.title);
}).catchError((e) => print(BmobError.convert(e).error));
Update
final blog = Blog()
..objectId = objectId
..title = '修改标题';
blog.update().then((BmobUpdated u) => print(u.updatedAt));
Delete
Blog()..objectId = objectId
..delete()
.then((BmobHandled h) => print(h.msg));
Delete a field value
Blog()..objectId = objectId
..deleteFieldValue('content')
.then((BmobUpdated u) => print(u.updatedAt));
Conditional queries and pagination
final q = BmobQuery<Blog>();
q.addWhereEqualTo('title', '博客标题');
q.addWhereGreaterThan('like', 70);
q.setOrder('-createdAt'); // 逆序:字段前加 -
q.setLimit(10);
q.setSkip(0);
q.queryObjects().then((List<dynamic> data) {
final blogs = data.map((i) => Blog.fromJson(i)).toList();
});
| Comparison | Method |
|---|---|
| Equal to | addWhereEqualTo |
| Not equal to | addWhereNotEqualTo |
| Less than | addWhereLessThan |
| Less than or equal to | addWhereLessThanOrEqualTo |
| Greater than | addWhereGreaterThan |
| Greater than or equal to | addWhereGreaterThanOrEqualTo |
| Count | queryCount() → Future<int> |
For OR / AND compound queries, see references/query.md。
Pointer / relation
final blog = Blog()..title = '带作者';
final user = BmobUser()..objectId = '已有用户的objectId';
blog.author = user;
blog.save();
// 查询:q.setInclude('author');
// 解除关联:blog.deleteFieldValue('author')
See references/pointer-and-relation.md。
User / SMS / file (brief table in skill, P1 will have dedicated auth/storage)
| Scenario | Entry |
|---|---|
| Sign up | BmobUser()..username=.. ..password=.. → .register() |
| Username-password login | .login() |
| SMS login | BmobSms.sendSms() → loginBySms(code) |
| Upload file | BmobFileManager.upload(file) → write to the table's BmobFile field |
See the full snippet in Flutter documentation "User Operations" "File Operations"。
Integration with MCP
If already configured Bmob MCP, before writing Flutter code first get_project_tables, to avoid misspelled field names and incorrect Pointer formats in schemaless mode.
Troubleshooting Quick Reference
For cross-platform symptoms first check shared/faq.md。
| Symptom | Troubleshooting |
|---|---|
| Request fails after initialization | Whether the Secret Key / API security code matches the console; whether you forgot the release resetDomain |
| Update / delete ineffective | Not set objectId |
| author is empty after include | setInclude field name matches the table field; the Pointer target objectId exists |
BmobError code unknown |
bmob-error-codes + REST/Android table comparison |
| Live query type error | onDataChanged use in Map, do not directly Blog.fromJson the whole package |
Advanced capabilities (read references/ as needed)
| Topic | Path |
|---|---|
| End-to-end scenarios | shared/recipes/ |
| BmobDocs sync code snippets | references/snippets/ |
| Model inheritance + JSON serialization | references/model-and-init.md |
| Conditional / OR·AND / statistics / count queries | references/query.md |
| Pointer / Relation / ACL / Role | references/pointer-and-relation.md |
Reference
- Full API:mds/data/flutter/index.md
- Raw(agent fetch):
metadata.docs_raw - Plugin source:https://github.com/bmob/bmob-flutter-sdk/tree/master/data_plugin
- Example project:https://github.com/bmob/bmob-flutter-sdk/tree/master/data_demo
- Operation routing:
shared/operation-routing.md - Error codes:
bmob-error-codes - MCP:
bmob-mcp