Last active
May 31, 2018 16:34
-
-
Save atreeon/7bc7c62239e009e5195ed4576e7099e8 to your computer and use it in GitHub Desktop.
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
/* | |
Firebase Security Rules | |
{ | |
"rules": { | |
".read": "auth != null", | |
} | |
} | |
*/ | |
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:firebase_database/firebase_database.dart'; | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
import 'googleSettings.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new HelloWorld(), | |
); | |
} | |
} | |
class HelloWorld extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('Login'), | |
), | |
body: new Column(children: [ | |
new RaisedButton( | |
child: new Text('Login'), | |
onPressed: () { | |
_login(); | |
}, | |
), | |
new RaisedButton( | |
child: new Text('Go to data page'), | |
onPressed: () { | |
Navigator.push( | |
context, | |
new MaterialPageRoute(builder: (context) => new PopsiclesScreen()), | |
); | |
}, | |
) | |
])); | |
} | |
void _login() async { | |
final FirebaseAuth _auth = FirebaseAuth.instance; | |
final GoogleSignIn _googleSignIn = new GoogleSignIn(); | |
//do auth | |
var googleAccount = await _googleSignIn.signIn(); | |
var googleAuth = await googleAccount.authentication; | |
await _auth.signInWithGoogle( | |
accessToken: googleAuth.accessToken, | |
idToken: googleAuth.idToken, | |
); | |
} | |
} | |
class PopsiclesScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new Container( | |
child: new FutureBuilder<List<String>>( | |
future: _getPopsicles(), // a Future<String> or null | |
builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) { | |
switch (snapshot.connectionState) { | |
case ConnectionState.none: | |
return new Text('Press button to start'); | |
case ConnectionState.waiting: | |
return new Text('Awaiting result...'); | |
case ConnectionState.done: | |
var widgets = snapshot.data.map((f) => new Text(f)); | |
return new ListView( | |
children: widgets.toList(), | |
); | |
default: | |
if (snapshot.hasError) | |
return new Text('Error: ${snapshot.error}'); | |
else | |
return new Text('Result: ${snapshot.data}'); | |
} | |
}, | |
)); | |
} | |
Future<List<String>> _getPopsicles() async { | |
//setup db | |
var firebaseApp = await FirebaseApp.configure( | |
name: 'db2', | |
options: const FirebaseOptions( | |
googleAppID: GoogleSettings.googleAppID, | |
apiKey: GoogleSettings.apiKey, | |
databaseURL: GoogleSettings.databaseURL, | |
), | |
); | |
var db = new FirebaseDatabase(app: firebaseApp); | |
//get data | |
var popsicles = new List<String>(); | |
var popsicleRef = db.reference().child('popcicles'); | |
var result = await popsicleRef.once(); | |
for (var item in result.value) { | |
if (item != null) { | |
popsicles.add(item.toString()); | |
} | |
} | |
return popsicles; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment