Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:firebase_database/firebase_database.dart';
- import 'package:firebase_database/ui/firebase_animated_list.dart';
- class CarnivalCompassHome extends StatefulWidget {
- @override
- CarnivalCompassHomeState createState() => new CarnivalCompassHomeState();
- }
- class CarnivalCompassHomeState extends State<CarnivalCompassHome> {
- final DatabaseReference _bandRef =
- FirebaseDatabase.instance.reference().child('bands');
- final DatabaseReference _messagesRef =
- FirebaseDatabase.instance.reference().child('messages');
- StreamSubscription<Event> _bandSubscription;
- StreamSubscription<Event> _messagesSubscription;
- bool _anchorToBottom = false;
- String _kTestKey = 'Hello';
- String _kTestValue = 'world!';
- @override
- void initState() {
- super.initState();
- FirebaseAuth.instance.currentUser().then((FirebaseUser firebaseUser) {
- if (firebaseUser == null) {
- FirebaseAuth.instance.signInAnonymously();
- print('Signing in anonymously');
- } else {
- print('Already signed in anonymously');
- }
- });
- FirebaseDatabase.instance.setPersistenceEnabled(true);
- FirebaseDatabase.instance.setPersistenceCacheSizeBytes(10000000);
- _bandRef.keepSynced(true);
- _bandSubscription = _bandRef.onValue.listen((Event event) {
- //setState(() {
- //_counter = event.snapshot.value ?? 0;
- //});
- });
- _messagesSubscription =
- _messagesRef.limitToLast(10).onChildAdded.listen((Event event) {
- print('Child added: ${event.snapshot.value}');
- });
- }
- @override
- void dispose() {
- super.dispose();
- _messagesSubscription.cancel();
- _bandSubscription.cancel();
- }
- @override
- Widget build(BuildContext context) {
- return new Scaffold(
- appBar: new AppBar(
- title: new Text('Carnival Compass'),
- ),
- body: new Column(
- children: <Widget>[
- new Text('i am working'),
- new Flexible(
- child: new FirebaseAnimatedList(
- key: new ValueKey<bool>(_anchorToBottom),
- query: _messagesRef,
- reverse: _anchorToBottom,
- sort: _anchorToBottom
- ? (DataSnapshot a, DataSnapshot b) => b.key.compareTo(a.key)
- : null,
- itemBuilder: (BuildContext context, DataSnapshot snapshot,
- Animation<double> animation, int index) {
- return new SizeTransition(
- sizeFactor: animation,
- child: new Text("Here: $index: ${snapshot.value.toString()}"),
- );
- },
- ),
- ),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement