Created
November 21, 2017 18:34
-
-
Save MarcinusX/0a90b9126daea03520a10773bae25ca4 to your computer and use it in GitHub Desktop.
Test for FirebaseUser update
This file contains 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 'dart:async'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.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 MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
final auth = FirebaseAuth.instance; | |
class _MyHomePageState extends State<MyHomePage> { | |
String _displayName = "Old display name - should be changed"; | |
String _photoUrl = "Old photoUrl - should be changed to null"; | |
Future<Null> _ensureLoggedIn() async { | |
await auth.signInAnonymously(); | |
UserUpdateInfo userUpdateInfo = new UserUpdateInfo() | |
..displayName = _displayName == "New display name" ? null : "New display name"; | |
await auth.updateProfile(userUpdateInfo); | |
FirebaseUser firebaseUser = await auth.currentUser(); | |
_displayName = firebaseUser.displayName; | |
_photoUrl = firebaseUser.photoUrl; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(widget.title), | |
), | |
body: new Center( | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
new Text(_displayName??"Null"), | |
new Text(_photoUrl??"Null"), | |
], | |
), | |
), | |
floatingActionButton: new FloatingActionButton( | |
child: new Icon(Icons.add), | |
onPressed: () { | |
_ensureLoggedIn(); | |
setState((){}); | |
}), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment