ncmb 2.7.0 ncmb: ^2.7.0 copied to clipboard
Dart and Flutter library for Nifcloud mobile backend(NCMB). Nifcloud mobile backend is a web service of mBaaS.
Example #
DataStore #
Create data #
NCMBObject item = NCMBObject('Item')
..set('msg', 'Hello World')
..set('array', ['a', 'b'])
..set('int', 1)
..set('name', 'Atsushi');
await item.save();
debugPrint(item.get('objectId'));
Update data #
await item.set('name', 'goofmint');
await item.save();
Delete data #
await item.delete();
Retribe data #
NCMBQuery query = NCMBQuery('Item');
// All data
var items = await query.fetchAll();
print(items[0].get('msg'));
// First data
var item2 = await query.fetch();
print(item2.get('msg'));
// Query (Equal)
query.equalTo('objectId', item.get('objectId'));
var item3 = await query.fetch();
print(item3.get('objectId'));
query.clear();
query
..notEqualTo('array', ['a', 'b', 'c'])
..limit(2)
..lessThan('int', 4);
items = await query.fetchAll();
print(items.length);
Retribete with type
// String
item.getString('fieldName');
// Int
item.getInt('fieldName');
// double
item.getDouble('fieldName');
// bool
item.getBool('fieldName');
// DateTime
item.getDateTime('fieldName');
// List
item.getList('fieldName');
You can use default value for there is no field.
// String
item.getString('fieldName', defaultValue = '');
// Int
item.getInt('fieldName', defaultValue = 0);
// double
item.getDouble('fieldName', defaultValue = 0);
// bool
item.getBool('fieldName', defaultValue = true);
// DateTime
item.getDateTime('fieldName', defaultValue = DateTime.now());
// List
item.getList('fieldName', defaultValue = ['']);
If there is no defaultValue and field, you will get Exception.
ACL #
var acl = NCMBAcl();
acl
..setPublicReadAccess(true)
..setPublicWriteAccess(false)
..setRoleWriteAccess('Admin', true)
..setUserWriteAccess('aaaaa', true);
item3.set('acl', acl);
Special type #
items[0]
// Class of Datastore
..set('item', items[1])
// DateTime
..set('time', DateTime.now())
// Geo location
..set('geo', NCMBGeoPoint(35.658611, 139.745556));
User #
Register #
var userName = 'aaa';
var password = 'bbb';
var user = NCMBUser();
user
..set('userName', userName)
..set('password', password);
await user.signUpByAccount();
Login #
var user = await NCMBUser.login(userName, password);
Anonymous Login #
var user = await NCMBUser.loginAsAnonymous();
Check session stats #
var user = await NCMBUser.currentUser();
if (user != null && (await user.enableSession())) {
print('Login');
} else {
print('no login');
await user.logout();
}
Logout #
user.logout();
File #
Upload #
Upload binary file
var fileName = 'dart.png';
var blob = await File(fileName).readAsBytes();
var file = await NCMBFile.upload(fileName, blob);
Upload text data as text file.
test("Upload text file", () async {
var fileName = 'dart.txt';
var file = await NCMBFile.upload(fileName, 'Hello world');
Custom mime type.
var fileName = 'dart.csv';
var file = await NCMBFile.upload(fileName, 'a,b,c', mimeType: 'text/csv');
Download #
var file = await NCMBFile.download('dart.png');
Script #
Common method #
var script = NCMBScript('script_test_get.js'); // Script name
script.query('hoge', 'fuga');
script.header('foo1', 'bar1');
script.header('foo2', 'bar2');
// Only POST or PUT method
script.body('hoge2', 'fuga2');
script.body('hoge3', 'fuga3');
GET #
var result = await script.get();
DELETE #
var result = await script.delete();
POST #
var result = await script.post();
PUT #
var result = await script.put();