authentification Firebase with Provider flutter

北城以北 提交于 2020-06-16 17:04:16

问题


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

Is there a way to check signing in inside Consumer?


回答1:


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



回答2:


Taking CopsOnRoad code,

class Auth with ChangeNotifier {
  bool _loggedIn = false;

  bool get loggedIn => _loggedIn;

  // other code
}

You can see, he already implemented loggedIn part, so all you need to do is

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

You may see in debug version (for few ms, TermsView showed and immediately that page will be replaced by HomeView). If you don't want that behavior, it's better to use Provider.of(...) inside a FutureBuilder, get the value of loggedIn and proceed.



来源:https://stackoverflow.com/questions/62085806/authentification-firebase-with-provider-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!