flutter - Flutte: update current user after signin with Firebase
问题描述
I am new to dart and flutter. And I am currently working on my signin function.
Things that I would like to do is that the I can go to the signin page when I clicked one of the button on the drawer, after I completed the firebase signin, it will get a current user and update the whole app.
main.dart:
void main()=> runApp(MyApp());
class MyApp extends StatelessWidget{
final ThemeData = _buildTheme();
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
title: 'MyFirstApp',
theme: ThemeData,
home: MainPage(),
);
}
}
class MainPage extends StatefulWidget{
const MainPage({Key key, this.currentUser}) : super(key: key);
final FirebaseUser currentUser;
@override
_MainPageState createState() => new _MainPageState();
}
class _MainPageState extends State<MainPage>{
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('MyFirstApp'),
),
body: BasicPage(),
drawer: LeftMenu(),
);
}
}
left_drawer_menu.dart:
final AuthService _fireAuthService = AuthService();
class LeftMenu extends StatefulWidget{
@override
LeftMenuState createState() => new LeftMenuState();
}
class LeftMenuState extends State<StatefulWidget>{
@override
Widget build(BuildContext context) {
FirebaseUser currentUsr = MainPage().currentUser;
return Drawer(
child: Column(
children: <Widget>[
...
if(currentUsr== null)(
ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AuthPage()),
);
},leading: Icon(Icons.account_circle),title: Text('Sign/Register'),)
)else(
ListTile(
onTap: () { _fireAuthService.signOut();
},
leading: Icon(Icons.account_circle),
title: Text('SignOut'),)
),
...
}
auth.dart:
class AuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
final Firestore firestore = Firestore.instance;
final CollectionReference collectionReference = Firestore.instance.collection('users');
FirebaseUser currentUser;
Future signInAnon() async{
AuthResult result = await _firebaseAuth.signInAnonymously();
FirebaseUser firebaseUser = result.user;
await updateUserLastSeen(firebaseUser);
currentUser = firebaseUser;
return currentUser;
}
Future signIn(String email, String password)async {
AuthResult result = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
FirebaseUser firebaseUser = result.user;
await updateUserLastSeen(firebaseUser);
currentUser = firebaseUser;
return currentUser;
}
...
}
AuthService fireAuthService = AuthService();
loginPage: Inside the login page, I use this after I click sigin button
RaisedButton(
child: Text('Sign in with Anon'),
onPressed: () async{
FirebaseUser result = await AuthService().signInAnon();
Navigator.push(context, MaterialPageRoute(builder: (context) => MainPage(currentUser: result)));
},
The signin function is working, but it cannot rebuild the whole app to tell that currentuser is exist
Am I doing this thing correctly, is there any good suggestion? Many thanks!!!
解决方案
U dont need to pass around current user down the widget tree.You can get current login user (after login successful) -:
FirebaseUser user = await FirebaseAuth.instance.currentUser();
u can use this anywhere to get the current login user :)
推荐阅读
- reactjs - TypeScript 错误:JSX 元素类型“MultiSelect”没有任何构造或调用签名
- videochat - 使用 agora 视频通话 API 时可以使用多个摄像头吗?
- python - 为什么 getattr 会捕获所有 AttributeErrors 而不仅仅是它正在寻找的属性?
- html - 带有纹理的自定义字体在标题中呈现没有纹理
- python - 将来自 API 的多个 JSON 读取组合到一个 JSON 文件中
- vue.js - 值未使用自定义选择组件更新
- typescript - 一种在 beforeEach 中初始化变量并进一步使用它的方法 - Mocha
- java - Freemarker 目录路径在 UAT 中不可读
- python - 如何在另一个中执行python文件然后在设定时间后关闭
- java - 从 kml 获取坐标时出错