带有提供商抖动的Firebase认证 [英] authentification Firebase with Provider flutter

查看:42
本文介绍了带有提供商抖动的Firebase认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在此处实施该身份验证带有Porvider的Firebase,这里的问题是,即使我关闭并响应应用程序,我也想保存变量loggingIn的状态,所以我使用了这段代码,但不起作用

I tried to implements this Authentification here Firebase with Porvider here the issue is I want to save the state of variable loggedIn even I close and reponning the app so I used this code but dont work

    class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<AuthenticationService>(builder: (_, auth, __) {
      if (auth.getCurrentUser() != null)
        return HomeView();
      else
        return TermsView();
    });
  }
}

这里获取当前用户的方法

here method to get current user

Future getCurrentUser() async {
FirebaseUser _user ;
_user = await FirebaseAuth.instance.currentUser();

return _user ;
}

是否可以检查Consumer内部的登录?

Is there a way to check signing in inside Consumer?

推荐答案

您需要使用FirebaseAuth.currentUser来检查用户是否登录,我正在Consumer内部使用FutureBuilder进行此项工作.

You need to use FirebaseAuth.currentUser to check if user is signed in or not, I am using FutureBuilder inside Consumer for this work.

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(
    ChangeNotifierProvider<Auth>(
      create: (_) => Auth(),
      child: MaterialApp(home: MyApp()),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<Auth>(
      builder: (_, auth, __) {
        return FutureBuilder<FirebaseUser>(
          future: auth.currentUser,
          builder: (context, snapshot) {
            if (snapshot.hasData) return HomeScreen();
            return LoginScreen();
          },
        );
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFF4C3E13),
      appBar: AppBar(title: Text('Home Screen')),
      floatingActionButton: FloatingActionButton.extended(
        label: Text('Sign out'),
        onPressed: () async {
          final auth = Provider.of<Auth>(context, listen: false);
          await auth.signOut();
        },
      ),
    );
  }
}

class LoginScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Login Screen')),
      body: Center(
        child: RaisedButton(
          onPressed: () async {
            final auth = Provider.of<Auth>(context, listen: false);
            await auth.signIn(email: 'user@user.com', password: 'user1234');
          },
          child: Text('Sign In'),
        ),
      ),
    );
  }
}

class Auth with ChangeNotifier {
  final _auth = FirebaseAuth.instance;

  Future<void> signOut() async {
    await _auth.signOut();
    notifyListeners();
  }

  Future<void> signIn({@required String email, @required String password}) async {
    await _auth.signInWithEmailAndPassword(email: email, password: password);
    notifyListeners();
  }

  Future<FirebaseUser> get currentUser async {
    return await _auth.currentUser();
  }
}

这篇关于带有提供商抖动的Firebase认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆