Skip to content

Commit 2015281

Browse files
authored
fix(database, Android): resolve limit modifier type casting (#17867)
* fix(database, Android): resolve limit modifier type casting * test: add tests for limitToFirst and limitToLast stream emissions
1 parent 906d703 commit 2015281

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/firebase_database/firebase_database/android/src/main/kotlin/io/flutter/plugins/firebase/database/FirebaseDatabasePlugin.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,11 +939,11 @@ class FirebaseDatabasePlugin :
939939
"limit" -> {
940940
when (modifier["name"] as String) {
941941
"limitToFirst" -> {
942-
val value = modifier["limit"] as Int
942+
val value = (modifier["limit"] as Number).toInt()
943943
query = query.limitToFirst(value)
944944
}
945945
"limitToLast" -> {
946-
val value = modifier["limit"] as Int
946+
val value = (modifier["limit"] as Number).toInt()
947947
query = query.limitToLast(value)
948948
}
949949
}

tests/integration_test/firebase_database/query_e2e.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ void setupQueryTests() {
226226

227227
expect(snapshot.value, isNull);
228228
});
229+
230+
test('streams emit limited maps', () async {
231+
await ref.set({
232+
'a': 'foo',
233+
'b': 'bar',
234+
'c': 'baz',
235+
});
236+
237+
final event = await ref.orderByKey().limitToFirst(2).onValue.first;
238+
239+
expect(
240+
event.snapshot.value,
241+
equals({
242+
'a': 'foo',
243+
'b': 'bar',
244+
}),
245+
);
246+
});
229247
});
230248

231249
group('limitToLast', () {
@@ -266,6 +284,24 @@ void setupQueryTests() {
266284

267285
expect(snapshot.value, isNull);
268286
});
287+
288+
test('streams emit limited maps', () async {
289+
await ref.set({
290+
'a': 'foo',
291+
'b': 'bar',
292+
'c': 'baz',
293+
});
294+
295+
final event = await ref.orderByKey().limitToLast(2).onValue.first;
296+
297+
expect(
298+
event.snapshot.value,
299+
equals({
300+
'b': 'bar',
301+
'c': 'baz',
302+
}),
303+
);
304+
});
269305
});
270306

271307
group('orderByChild', () {

0 commit comments

Comments
 (0)