Skip to content

Instantly share code, notes, and snippets.

@coxato
Created January 16, 2021 07:43
Show Gist options
  • Save coxato/ef7e0cd3c1b8019e329d5806de94ffdb to your computer and use it in GitHub Desktop.
Save coxato/ef7e0cd3c1b8019e329d5806de94ffdb to your computer and use it in GitHub Desktop.
firestore: get data from db with dart, and dynamically pass named params - v1
/*
Carlos Martinez 2021
this is a very little library for confortable use of firestore in dart.
version 1.0
*/
import 'package:cloud_firestore/cloud_firestore.dart';
// class for firestore `where` method, this is a trick
// because Dart doest not support pass dynamically named parameters
// so I use a simple class like a javascript plain object, to can use dot notation:
class FireWhere{
static const String isEqualTo = "isEqualTo";
static const String isNotEqualTo = "isNotEqualTo";
static const String isLessThan = "isLessThan";
static const String isLessThanOrEqualTo = "isLessThanOrEqualTo";
static const String isGreaterThan = "isGreaterThan";
static const String isGreaterThanOrEqualTo = "isGreaterThanOrEqualTo";
static const String arrayContains = "arrayContains";
static const String arrayContainsAny = "arrayContainsAny";
static const String whereIn = "whereIn";
static const String whereNotIn = "whereNotIn";
static const String isNull = "isNull";
}
class FirestoreDB {
final db = FirebaseFirestore.instance;
CollectionReference getRef(String collection) => db.collection(collection);
Future<List<Map<String, dynamic>>> getData(
String collection,
{
String whereField = '',
Map<String, dynamic> where,
int limit,
String orderBy,
bool orderDescending = false
}
) async{
List<Map<String, dynamic>> docsList = new List<Map<String, dynamic>>();
dynamic ref = getRef(collection);
// Dart doest not support pass dynamically named parameters
// so I do things like this:
if(whereField.length > 0){
ref = ref.where(
whereField,
// if the value in Map is null, is ignored by firestore `where` method
isEqualTo: where[FireWhere.isEqualTo],
isGreaterThan: where[FireWhere.isGreaterThan],
isGreaterThanOrEqualTo: where[FireWhere.isGreaterThanOrEqualTo],
isLessThan: where[FireWhere.isLessThan],
isLessThanOrEqualTo: where[FireWhere.isLessThanOrEqualTo],
isNotEqualTo: where[FireWhere.isNotEqualTo],
isNull: where[FireWhere.isNull],
whereIn: where[FireWhere.whereIn],
whereNotIn: where[FireWhere.whereNotIn],
arrayContains: where[FireWhere.arrayContains],
arrayContainsAny: where[FireWhere.arrayContainsAny]
);
}
if(limit != null) ref = ref.limit(limit);
if(orderBy != null) ref = ref.orderBy(orderBy, descending: orderDescending);
QuerySnapshot data = await ref.get();
// save data in a list
data.docs.forEach((element) {
docsList.add({
...element.data(),
"id": element.id
});
});
return docsList;
}
}
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import './Firebase/database.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// ========== api use ==================
var myDocs = await FirestoreDB().getData(
'numbers',
whereField: 'n',
where: { FireWhere.isLessThan: 18 },
limit: 1
);
print(myDocs);
// =====================================
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("Hi");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment