首页 > 解决方案 > 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!!!

标签: flutterdartfirebase-authentication

解决方案


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 :)


推荐阅读