Skip to content

Instantly share code, notes, and snippets.

@dotdoom
Created July 15, 2018 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotdoom/5180eb27084afb3b71db44f2201c6877 to your computer and use it in GitHub Desktop.
Save dotdoom/5180eb27084afb3b71db44f2201c6877 to your computer and use it in GitHub Desktop.
Demo of FirebaseDatabase orderByChild with Flutter
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