Skip to content

Commit 3c8cfdf

Browse files
authored
#1385 support for firestore documentsnapshot (#1386)
Co-authored-by: kmahmood74 <[email protected]>
1 parent 4df4bae commit 3c8cfdf

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

lib/framework/apiproviders/firestore/firestore_api_provider.dart

+15-11
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ class FirestoreAPIProvider extends APIProvider with LiveAPIProvider {
149149
} else if (result is DocumentReference) {
150150
// Handle DocumentReference for 'add' operations
151151
body = {'id': result.id, 'path': result.path};
152-
} else if (result == null) {
152+
} else if (result is DocumentSnapshot) {
153153
// Handle void results for 'set', 'update', and 'delete' operations
154-
body = {'message': 'Operation completed successfully'};
155-
} else {
154+
body = {'document': getDocument(result)};
155+
} else if (result == null) {
156156
// Fallback for unexpected types, you might not need this, but it's here just in case
157157
body = {'message': 'Unknown response type', 'details': result.toString()};
158158
}
@@ -167,15 +167,17 @@ class FirestoreAPIProvider extends APIProvider with LiveAPIProvider {
167167

168168
List<Map<String, dynamic>> getDocuments(QuerySnapshot snapshot) {
169169
return snapshot.docs.map((doc) {
170-
// Create a new map from the document data
171-
Map<String, dynamic> data =
172-
convertFirestoreTypes(doc.data() as Map<String, dynamic>);
173-
// Add the document ID under a reserved/special key
174-
data['_documentId'] = doc.id;
175-
return data;
170+
return getDocument(doc);
176171
}).toList();
177172
}
178-
173+
Map<String, dynamic> getDocument(DocumentSnapshot doc) {
174+
// Create a new map from the document data
175+
Map<String, dynamic> data =
176+
convertFirestoreTypes(doc.data() as Map<String, dynamic>);
177+
// Add the document ID under a reserved/special key
178+
data['_documentId'] = doc.id;
179+
return data;
180+
}
179181
Map<String, dynamic> convertFirestoreTypes(Map<String, dynamic> input) {
180182
Map<String, dynamic> convert(Map<String, dynamic> map) {
181183
final Map<String, dynamic> newMap = {};
@@ -214,7 +216,9 @@ class FirestoreAPIProvider extends APIProvider with LiveAPIProvider {
214216
return item; // Return the item unchanged if it's not a Map, GeoPoint, or Timestamp
215217
}
216218
}).toList();
217-
} else {
219+
} else if (value is DocumentSnapshot){
220+
newMap[key] = getDocument(value);
221+
} else{
218222
// For all other types, just copy the value
219223
newMap[key] = value;
220224
}

lib/framework/apiproviders/firestore/firestore_types.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ class FirestoreTimestamp with Invokable {
157157
return {
158158
'seconds': () => timestamp.seconds,
159159
'nanoseconds': () => timestamp.nanoseconds,
160-
'toDate': () => Date(timestamp.toDate()),
161160
};
162161
}
163162

@@ -168,6 +167,11 @@ class FirestoreTimestamp with Invokable {
168167
Map<String, Function> methods() {
169168
return {
170169
'toDate': () => Date(timestamp.toDate()),
170+
'toString': () => timestamp.toDate().toString(),
171171
};
172172
}
173+
@override
174+
String toString() {
175+
return methods()['toString']!();
176+
}
173177
}

0 commit comments

Comments
 (0)