Firebase Resources & Complex queries in Firestore using Dart & Flutter

Sajid Ahmed
3 min readJan 27, 2020

1) Firebase integration in flutter:

https://medium.com/@themaaz32/firebase-integration-in-flutter-application-bb1c5f4a0eb9

2) Basics about cloud Firestore with Flutter:

https://medium.com/@atul.sharma_94062/how-to-use-cloud-firestore-with-flutter-e6f9e8821b27

3) Algolia search in Firestore using TextBox:

https://itnext.io/full-text-search-in-flutter-with-algolia-firestore-cloud-functions-with-optimization-54004d727ad1

4) Write data in array of objects in Firestore:

Case-1(Overwrite a field inside of map in list):

Future<void> updateUserData(String bus_id, String speed, GeoPoint geoPoint) async {

Map<String, dynamic>data=new Map();
data[‘coordinate’]=geoPoint;
data[‘velocity’]=speed;

var list=new List<Map<String, dynamic>>();
list.add(data);

firestoreref.document(bus_id).setData({
‘locations’: FieldValue.arrayUnion(list)
});

}

Case-2(Append the data in array):

Future<void> updateUserData(String bus_id, String speed, GeoPoint geoPoint) async {

Map<String, dynamic>data=new Map();
data[‘coordinate’]=geoPoint;
data[‘velocity’]=speed;

var list=new List<Map<String, dynamic>>();
list.add(data);
firestoreref.document(bus_id).setData({
‘locations’: FieldValue.arrayUnion(list)
},merge:true);

}

For using updateData() , you need to access a existing document ,otherwise you cant write the data.

Using setData() ,you can write both existing and non-existing document to write some data.

Merge:true ←- parameter is used for appending a existing data.

Read Data:

Fetch all the Array of objects of all documents:

Future<void> readLoacationData() async
{
var query = await Firestore.instance.collection(‘crowd_sourcing’).getDocuments();

query.documents.forEach((doc) {

List<Map<dynamic, dynamic>> values = List.from(doc.data[‘locations’]);
});
}

Fetch array of objects of a single document:

Future<void> readLoacationData() async
{
var query = await Firestore.instance.collection(‘crowd_sourcing’).document(‘bus-1’);

query.snapshots().forEach((doc) {

List<Map<dynamic, dynamic>> values = List.from(doc.data[‘locations’]);

});

Listener function in any Document Changes:

Structure-1

Future<void> readLoacationData() async
{
var query = await Firestore.instance.collection(‘crowd_sourcing’);

query.snapshots().listen((querySnapshot) {
querySnapshot.documentChanges.forEach((change) {

List<Map<dynamic, dynamic>>values=

List.from(change.document.data[‘locations’]);
});

});

}

Structure-2

Future<void> readLoacationData() async
{
var query = await Firestore.instance.collection(‘bus_location’);

query.snapshots().listen((querySnapshot) {
querySnapshot.documentChanges.forEach((change) {

Map<dynamic, dynamic> values = Map.from(change.document.data);
});
});

}

Whenever a value is updated in any of these documents,the listener function is called and the data of array of objects are updated in values list of map.(Structure-1).

Whenever a value is updated in any of these documents,the listener function is called and the data are updated in values of map. (Structure-2)

Listener function in single Document Changes:

Structure-1:

Future<void> readLoacationData() async
{
var query = await Firestore.instance.collection(‘crowd_sourcing’).document(‘bus-1’);

List<Map<dynamic, dynamic>> main_list=new List<Map<dynamic, dynamic>>();

query.snapshots().listen((querySnapshot) {

List<Map<dynamic, dynamic>> values = List.from(querySnapshot.data[‘locations’]);

});

--

--