mongodb_mobile 0.0.1 mongodb_mobile: ^0.0.1 copied to clipboard
A new flutter plugin project.
import 'dart:async';
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mongodb_mobile/carrier.dart';
import 'package:mongodb_mobile/mongodb_mobile.dart';
void main() {
debugPrint(
'π π π Flutter MongoDB Mobile Platform Example App starting ... : π§©π§©π§©');
runApp(MongoExampleApp());
}
class MongoExampleApp extends StatefulWidget {
@override
_MongoExampleAppState createState() => _MongoExampleAppState();
}
class _MongoExampleAppState extends State<MongoExampleApp> {
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await MongodbMobile.platformVersion;
debugPrint('_MyAppState: π§©π§©π§© Platform Version : π $platformVersion');
var res = await MongodbMobile.setAppID({
'appID': 'exampleApp',
'type': 'local',
});
print(res);
} on PlatformException catch (f) {
print('πΏπΏπΏπΏπΏπΏπΏπΏ PlatformException π π π - $f');
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {});
}
Random random = Random(DateTime.now().millisecondsSinceEpoch);
/// Add document to a collection
Future insertDocument() async {
debugPrint('\n\nπ π inserting a typical document ....');
dynamic result;
try {
var carrier = Carrier(db: 'testdb', collection: 'testCollection', data: {
'name': 'Malia',
'lastName': 'Obama',
'wealth': random.nextInt(10000) * 1.04,
'date': DateTime.now().toUtc().toIso8601String(),
'desc': 'π π π serve with purpose π π π'
});
result = await MongodbMobile.insert(carrier);
debugPrint(
'\n\nπ§©π§©π§©π§©π§©π§© _MyAppState: insertDocument π§©π§©π§© document added : π statusCode: $result\n\n\n');
showSnackbar(
message: ' π§©π§©π§© Document inserted',
scaffoldKey: _key,
backgroundColor: Colors.black,
textColor: Colors.white);
} on PlatformException catch (f) {
print('πΏπΏπΏπΏπΏπΏπΏπΏ PlatformException π π π - $f');
result = 'Failed to get platform version.';
var d =
"\ud83d\udc99\ud83d\udc99\ud83d\udc99 serve with purpose \ud83d\udc99\ud83d\udc99\ud83d\udc99";
}
}
List documents = List();
/// Get all documents from a collection
Future getAllDocuments() async {
debugPrint('\n\nπ π getAllDocuments ....');
try {
var carrier = Carrier(db: 'testdb', collection: 'testCollection');
documents = await MongodbMobile.getAll(carrier);
debugPrint(
'\n\nπ π π _MyAppState: getAllDocuments π§©π§©π§© retrieved : π ${documents.length} documents π \n\n\n');
showSnackbar(
message: 'π π π ${documents.length} documents found',
scaffoldKey: _key,
backgroundColor: Colors.black,
textColor: Colors.white);
} on PlatformException catch (f) {
print('πΏπΏπΏπΏπΏπΏπΏπΏ PlatformException π π π - $f');
}
}
/// Delete document from a collection
Future delete() async {
debugPrint('\n\nπ π delete ....');
try {
var carrier = Carrier(
db: 'testdb',
collection: 'testCollection',
id: '5cf8a27c6bc83124d1e937b0');
var res = await MongodbMobile.delete(carrier);
debugPrint(
'\n\nπ π π _MyAppState:delete: π§©π§©π§© deleted : π : $res π \n\n\n');
showSnackbar(
message: 'π π π document deleted',
scaffoldKey: _key,
backgroundColor: Colors.black,
textColor: Colors.white);
} on PlatformException catch (f) {
print('πΏπΏπΏπΏπΏπΏπΏπΏ PlatformException π π π - $f');
}
}
/// Delete document from a collection
Future getOne() async {
debugPrint('\n\nπ π get one doc ....');
try {
var carrier = Carrier(
db: 'testdb',
collection: 'testCollection',
id: '5cf8e4aa6bc8315dd0a51755');
var res = await MongodbMobile.getOne(carrier);
debugPrint(
'\n\nπ π π _MyAppState:delete: π§©π§©π§© get one : π : $res π \n\n\n');
showSnackbar(
message: 'π π π document retrieved',
scaffoldKey: _key,
backgroundColor: Colors.black,
textColor: Colors.white);
} on PlatformException catch (f) {
print('πΏπΏπΏπΏπΏπΏπΏπΏ PlatformException π π π - $f');
}
}
/// Replace document from a collection
Future replace() async {
debugPrint('\n\nπ π replace ....');
try {
var carrier = Carrier(
db: 'testdb',
collection: 'testCollection',
id: '5cf8a2206bc83124d1e93787',
data: {
'name': 'Barack',
'lastName': 'Obama',
'wealth': 8880080.98,
'date': DateTime.now().toUtc().toIso8601String(),
'desc': 'π serve with UPDATED purpose π'
});
var res = await MongodbMobile.replace(carrier);
debugPrint(
'\n\nπ π π _MyAppState:replace: π§©π§©π§© replaced : π 1 document : $res π \n\n\n');
showSnackbar(
message: 'π π π document replaced',
scaffoldKey: _key,
backgroundColor: Colors.indigo.shade800,
textColor: Colors.white);
} on PlatformException catch (f) {
print('πΏπΏπΏπΏπΏπΏπΏπΏ PlatformException π π π - $f');
}
}
/// Query Mongo database using collection properties
Future query() async {
debugPrint('\n\nπ π getByProperty ....');
try {
var carrier = Carrier(db: 'testdb', collection: 'testCollection', query: {
"gt": {"wealth": 90000},
"eq": {"lastName": "Obama"},
"and": true,
"or": false,
"limit": 0
});
var object = await MongodbMobile.query(carrier);
debugPrint(
'\n\nπ π π _MyAppState: query: π§©π§©π§© retrieved : π ${object.length} documents π see below: \n\n\n');
print(object);
showSnackbar(
message: ' π π π ${object.length} documents found',
scaffoldKey: _key,
backgroundColor: Colors.purple,
textColor: Colors.white);
} on PlatformException catch (f) {
print('πΏπΏπΏπΏπΏπΏπΏπΏ PlatformException π π π - $f');
}
}
void showSnackbar(
{@required GlobalKey<ScaffoldState> scaffoldKey,
@required String message,
@required Color textColor,
@required Color backgroundColor}) {
if (scaffoldKey.currentState == null) {
print('AppSnackbar.showSnackbar --- currentState is NULL, quit ..');
return;
}
scaffoldKey.currentState.removeCurrentSnackBar();
scaffoldKey.currentState.showSnackBar(new SnackBar(
content: _getText(message, textColor),
duration: new Duration(seconds: 15),
backgroundColor: backgroundColor,
));
}
static Widget _getText(
String message,
Color textColor,
) {
return Text(
message,
overflow: TextOverflow.clip,
style: new TextStyle(color: textColor),
);
}
GlobalKey<ScaffoldState> _key = GlobalKey();
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
key: _key,
appBar: AppBar(
title: const Text('MongoDB Plugin Example App'),
backgroundColor: Colors.deepOrange.shade300,
bottom: PreferredSize(
preferredSize: Size.fromHeight(100),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Text(
'Checking out Flutter MongoDB Plugin',
style: TextStyle(color: Colors.white),
),
],
),
),
),
),
backgroundColor: Colors.brown.shade50,
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.bug_report),
backgroundColor: Colors.red.shade700,
elevation: 24,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Card(
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: ListView(
children: <Widget>[
Container(
width: 300,
child: RaisedButton(
onPressed: insertDocument,
elevation: 16,
color: Colors.pink.shade300,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
'Insert One Document',
style: TextStyle(color: Colors.white),
),
),
),
),
SizedBox(
height: 20,
),
Container(
width: 300,
child: RaisedButton(
onPressed: getAllDocuments,
elevation: 16,
color: Colors.purple.shade300,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
'Get All Documents',
style: TextStyle(color: Colors.white),
),
),
),
),
SizedBox(
height: 20,
),
Container(
width: 300,
child: RaisedButton(
onPressed: query,
elevation: 16,
color: Colors.teal.shade300,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
'Query By Properties',
style: TextStyle(color: Colors.white),
),
),
),
),
SizedBox(
height: 20,
),
Container(
width: 300,
child: RaisedButton(
onPressed: delete,
elevation: 16,
color: Colors.blue.shade400,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
'Delete Document',
style: TextStyle(color: Colors.white),
),
),
),
),
SizedBox(
height: 20,
),
Container(
width: 300,
child: RaisedButton(
onPressed: replace,
elevation: 16,
color: Colors.brown.shade400,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
'Replace Document',
style: TextStyle(color: Colors.white),
),
),
),
),
SizedBox(
height: 20,
),
Container(
width: 300,
child: RaisedButton(
onPressed: getOne,
elevation: 16,
color: Colors.indigo.shade400,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
'Get One Document',
style: TextStyle(color: Colors.white),
),
),
),
),
],
),
),
),
),
),
),
);
}
}