diff --git a/packages/cloud_firestore/test/cloud_firestore_test.dart b/packages/cloud_firestore/test/cloud_firestore_test.dart index 7ff20c15b306..d0ce2d7c885a 100755 --- a/packages/cloud_firestore/test/cloud_firestore_test.dart +++ b/packages/cloud_firestore/test/cloud_firestore_test.dart @@ -42,40 +42,48 @@ void main() { switch (methodCall.method) { case 'Query#addSnapshotListener': final int handle = mockHandleId++; - BinaryMessages.handlePlatformMessage( - Firestore.channel.name, - Firestore.channel.codec.encodeMethodCall( - new MethodCall('QuerySnapshot', { - 'app': app.name, - 'handle': handle, - 'paths': ["${methodCall.arguments['path']}/0"], - 'documents': [kMockDocumentSnapshotData], - 'documentChanges': [ - { - 'oldIndex': -1, - 'newIndex': 0, - 'type': 'DocumentChangeType.added', - 'document': kMockDocumentSnapshotData, - }, - ], - }), - ), - (_) {}, - ); + // Wait for a microtask before sending a message back. + // Otherwise the first request didn't have the time to finish. + scheduleMicrotask(() { + BinaryMessages.handlePlatformMessage( + Firestore.channel.name, + Firestore.channel.codec.encodeMethodCall( + new MethodCall('QuerySnapshot', { + 'app': app.name, + 'handle': handle, + 'paths': ["${methodCall.arguments['path']}/0"], + 'documents': [kMockDocumentSnapshotData], + 'documentChanges': [ + { + 'oldIndex': -1, + 'newIndex': 0, + 'type': 'DocumentChangeType.added', + 'document': kMockDocumentSnapshotData, + }, + ], + }), + ), + (_) {}, + ); + }); return handle; case 'Query#addDocumentListener': final int handle = mockHandleId++; - BinaryMessages.handlePlatformMessage( - Firestore.channel.name, - Firestore.channel.codec.encodeMethodCall( - new MethodCall('DocumentSnapshot', { - 'handle': handle, - 'path': methodCall.arguments['path'], - 'data': kMockDocumentSnapshotData, - }), - ), - (_) {}, - ); + // Wait for a microtask before sending a message back. + // Otherwise the first request didn't have the time to finish. + scheduleMicrotask(() { + BinaryMessages.handlePlatformMessage( + Firestore.channel.name, + Firestore.channel.codec.encodeMethodCall( + new MethodCall('DocumentSnapshot', { + 'handle': handle, + 'path': methodCall.arguments['path'], + 'data': kMockDocumentSnapshotData, + }), + ), + (_) {}, + ); + }); return handle; case 'Query#getDocuments': return {