带有提供商抖动的Firebase认证

时间:2020-05-29 12:18:18

标签: firebase flutter firebase-authentication

我尝试使用Porvider来实现此身份验证here Firebase,问题是即使关闭并响应该应用程序,我也想保存变量loggingIn的状态,所以我使用了此代码,但不起作用

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

这里获取当前用户的方法

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

return _user ;
}

有没有办法检查Consumer内部的登录?

2 个答案:

答案 0 :(得分:1)

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

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();
  }
}

答案 1 :(得分:0)

使用CopsOnRoad代码

class Auth with ChangeNotifier {
  bool _loggedIn = false;

  bool get loggedIn => _loggedIn;

  // other code
}

您可以看到,他已经实现了loggedIn部分,因此您所需要做的就是

return Consumer<Auth>(
  builder: (_, auth, __) {
    if (auth.loggedIn)
      return HomeView();
    else
      return TermsView();
  },
);

您可能会在调试版本中看到它(显示了几毫秒,TermsView,然后该页面立即被HomeView替换)。如果您不希望出现这种情况,最好在Provider.of(...)中使用FutureBuilder,获取loggedIn的值并继续。