Created
August 24, 2023 02:02
-
-
Save jauntybrain/dd163efc70ec3ee2e32abba18e983060 to your computer and use it in GitHub Desktop.
Detected objects stream controller from Firestore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StreamController<DetectedObject?> controller = StreamController<DetectedObject?>(); | |
void dispose() { | |
controller.close(); | |
} | |
void handle(DetectedObject data) async { | |
// Your handling logic for the detected object | |
} | |
// Modify this to match your DetectedObject structure | |
CollectionReference<DetectedObject> collectionRef = firestore.collection(PATH).withConverter<DetectedObject>( | |
fromFirestore: (snapshot, _) => DetectedObject.fromJson(snapshot.data()!), | |
toFirestore: (detectedObject, _) => detectedObject.toJson(), | |
); | |
collectionRef | |
.where('userID', isEqualTo: USER_ID) | |
// ...Other criteria | |
.orderBy('date', descending: true) | |
.limit(1) | |
.snapshots(includeMetadataChanges: false) | |
.asyncMap((event) async* { | |
for (var change in event.docChanges) { | |
final data = change.doc.data(); | |
final date = data?.date; | |
final fifteenSecondsAgo = DateTime.now().subtract(const Duration(seconds: 15)); | |
// Choose the most recent document | |
// (you can modify this based on your needs) | |
if (date != null && date.compareTo(fifteenSecondsAgo) > 0) { | |
yield data; | |
} | |
} | |
}) | |
.listen(controller.add); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment