使用提供程序对Firebase进行身份验证

使用提供程序对Firebase进行身份验证,firebase,flutter,firebase-authentication,Firebase,Flutter,Firebase Authentication,我试图用Porvider实现这个身份验证Firebase这里的问题是我想保存变量loggedIn的状态,即使我关闭并重新规划应用程序,所以我使用了这个代码,但不起作用 class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return Consumer<AuthenticationService>(builder: (_, auth, __) {

我试图用Porvider实现这个身份验证Firebase这里的问题是我想保存变量loggedIn的状态,即使我关闭并重新规划应用程序,所以我使用了这个代码,但不起作用

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

有没有办法在
消费者
内部检查登录?

您需要使用
FirebaseAuth.currentUser
来检查用户是否登录,我正在使用
FutureBuilder
内部
消费者
来完成这项工作

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();
  }
}
void main(){
WidgetsFlutterBinding.ensureInitialized();
runApp(
变更通知提供者(
创建:()=>Auth(),
子项:MaterialApp(主项:MyApp()),
),
);
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
退货消费者(
建筑商:(u,auth,uu){
回归未来建设者(
未来:auth.currentUser,
生成器:(上下文,快照){
if(snapshot.hasData)返回主屏幕();
返回LoginScreen();
},
);
},
);
}
}
类主屏幕扩展无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色(0xFF4C3E13),
appBar:appBar(标题:文本(“主屏幕”),
floatingActionButton:floatingActionButton.extended(
标签:文本(“注销”),
onPressed:()异步{
final auth=Provider.of(上下文,listen:false);
等待auth.signOut();
},
),
);
}
}
类LoginScreen扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“登录屏幕”),
正文:中(
孩子:升起按钮(
onPressed:()异步{
final auth=Provider.of(上下文,listen:false);
等待授权登录(电子邮件:'user@user.com,密码:“user1234”);
},
子项:文本(“登录”),
),
),
);
}
}
使用ChangeNotifier类身份验证{
final _auth=FirebaseAuth.instance;
Future signOut()异步{
等待_auth.signOut();
notifyListeners();
}
未来登录({@required String email,@required String password})异步{
等待使用email和password(电子邮件:电子邮件,密码:password)进行身份验证登录;
notifyListeners();
}
未来获取当前用户异步{
返回wait_auth.currentUser();
}
}

非常感谢(TTTT:)
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();
  }
}