in_app_database 1.2.0 in_app_database: ^1.2.0 copied to clipboard
Efficient in-app database for seamless data storage and retrieval, ensuring optimal performance and user experience.
InAppDatabase #
INITIALIZATIONS: #
Future<void> main() async {
// Map<String, dynamic> db = {};
final db = await SharedPreferences.getInstance();
await InAppDatabase.init(
reader: (String key) async {
return db.getString(key);
// return db[key];
},
writer: (String key, String? value) async {
if (value != null) {
return db.setString(key, value);
// db[key] = value;
// return true;
} else {
return db.remove(key);
// db.remove(key);
// return true;
}
},
);
runApp(const MyApp());
}
USE CASES: #
Add collection document #
Future<InAppDocumentSnapshot?> addCollectionDocument() {
return InAppDatabase.i.collection("users").add({
"username": UserFaker.username,
"email": UserFaker.email,
"age": UserFaker.age,
"country": UserFaker.country,
"photoUrl": UserFaker.photoUrl,
});
}
Set new document #
Future<InAppDocumentSnapshot?> setDocument() {
return InAppDatabase.i.collection("users").doc("1").set({
"username": "This is a username",
"email": "This is a user email",
"age": 24,
"country": "Bangladesh",
"photoUrl": "null",
"hobbies": ['coding', 'gaming', 'sleeping'],
"skills": ['flutter', 'android', 'node.js', 'spring boot', 'etc'],
});
}
Update specific document #
Future<InAppDocumentSnapshot?> updateSpecificDocument() {
return InAppDatabase.i.collection("users").doc("1").update({
'username': "This is a updated username",
'email': "This is a updated user email",
'age': InAppFieldValue.increment(2),
'balance': InAppFieldValue.increment(-10.2),
'hobbies': InAppFieldValue.arrayUnion(['swimming']),
'skills': InAppFieldValue.arrayRemove(['node.js', 'spring boot']),
'timestamp': InAppFieldValue.serverTimestamp(),
'photoUrl': InAppFieldValue.delete(),
});
}
Delete specific document #
Future<bool> deleteSpecificDocument() {
return InAppDatabase.i.collection("users").doc("1").delete();
}
Get specific document #
Future<InAppDocumentSnapshot?> getSpecificDocument() {
return InAppDatabase.i.collection("users").doc("1").get();
}
Get all documents from collection #
Future<InAppQuerySnapshot> getAllDocuments() {
return InAppDatabase.i.collection("users").get();
}
Get specific documents by simple query #
Future<InAppQuerySnapshot> getSpecificDocumentsByQuery() {
return InAppDatabase.i
.collection("users")
.where("username", isEqualTo: "emma_smith")
.get();
}
Get specific documents by complex query #
Future<InAppQuerySnapshot> getSpecificDocumentsByQuery() {
return InAppDatabase.i
.collection("users")
.where(InAppFilter.or([
InAppFilter("username", isEqualTo: "emma_smith"),
InAppFilter("age", isGreaterThanOrEqualTo: 50),
]))
.where("age", isLessThanOrEqualTo: 60)
.orderBy("age", descending: false)
.orderBy("email", descending: false)
.limit(10)
.get();
}
Get all documents #
Stream<InAppQuerySnapshot> getCollectionSnapshots() {
return InAppDatabase.i.collection("users").snapshots();
}
Get specific documents by simple query #
Future<InAppQuerySnapshot> getSpecificDocumentsByQuerySnapshots() {
return InAppDatabase.i
.collection("users")
.where("username", isEqualTo: "emma_smith")
.snapshots();
}
Get specific documents by complex query #
Stream<InAppQuerySnapshot> getComplexQuerySnapshots() {
return InAppDatabase.i
.collection("users")
.where(InAppFilter.or([
InAppFilter("username", isEqualTo: "emma_smith"),
InAppFilter("age", isGreaterThanOrEqualTo: 50),
]))
.where("age", isLessThanOrEqualTo: 60)
.orderBy("age", descending: false)
.orderBy("email", descending: false)
.limit(10)
.snapshots();
}