Created
July 15, 2018 14:11
-
-
Save dotdoom/5180eb27084afb3b71db44f2201c6877 to your computer and use it in GitHub Desktop.
Demo of FirebaseDatabase orderByChild with Flutter
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
import 'package:firebase_database/firebase_database.dart'; | |
import 'package:flutter/foundation.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
const subpath = 'flutter-firebase-order-by-child'; | |
MyApp() { | |
demo(); | |
} | |
void demo() async { | |
await FirebaseDatabase.instance.reference().child(subpath).set({ | |
'Albert Einstein': { | |
'year': 1879, | |
}, | |
'Marie Curie': { | |
'year': 1867, | |
}, | |
'Nikola Tesla': { | |
'year': 1856, | |
}, | |
'William Shakespeare': { | |
'year': 1564, | |
}, | |
}); | |
debugPrint('Sorted years:'); | |
(await FirebaseDatabase.instance | |
.reference() | |
.child(subpath) | |
.orderByChild('year') | |
.limitToFirst(3) | |
.onValue | |
.first) | |
.snapshot | |
.value | |
.values | |
.forEach((v) => debugPrint(v['year'].toString())); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center(child: Text('It works!', textDirection: TextDirection.ltr)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment