[英]Flutter: How to set up Stream and Provider
I am making a Flutter app with Anonymous Sign In with Firebase (I'll add other sign in methods later).我正在制作一个 Flutter 应用程序,使用 Firebase 匿名登录(稍后我将添加其他登录方法)。 I want to create Stream and Provider so that if the user taps on Login button it takes the user to the Home() and if the user taps on Logout button inside Home() it takes it to Login() page.
我想创建 Stream 和提供程序,以便如果用户点击登录按钮,它会将用户带到 Home(),如果用户点击 Home() 内的注销按钮,它会将其带到 Login() 页面。
I also want to do this for app restarts like when the user has logged in previously the user should be taken directly to Home() page instead of Login().我还想为应用程序重新启动执行此操作,例如当用户之前登录时,用户应该直接进入 Home() 页面而不是 Login()。
I've very basic knowledge of all this.我对这一切都有非常基本的了解。 Here, I'm following the FlutterFire docs.
在这里,我正在关注 FlutterFire 文档。 https://firebase.flutter.dev/docs/auth/usage/#authentication-state In this link they have talked about Stream and all but I'm not getting.
https://firebase.flutter.dev/docs/auth/usage/#authentication-state在这个链接中,他们谈到了 Stream 和所有但我没有得到。
Here are my codes:这是我的代码:
main.dart main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Wrapper(),
);
}
}
wrapper.dart包装器.dart
class Wrapper extends StatefulWidget {
@override
_WrapperState createState() => _WrapperState();
}
class _WrapperState extends State<Wrapper> {
final FirebaseAuth _auth = FirebaseAuth.instance;
@override
Widget build(BuildContext context) {
_auth.authStateChanges().listen((User user) {
if (user == null) {
print('the user is currently signed out');
return Authenticate();
} else {
print('The User is currently signed In');
return Home();
}
});
}
}
AuthService身份验证服务
import 'package:firebase_auth/firebase_auth.dart';
class AuthenticationService {
final FirebaseAuth _auth = FirebaseAuth.instance;
//sign in anonympusly
Future signinanon() async {
try {
UserCredential result = await _auth.signInAnonymously();
User userdetails = result.user;
return userdetails;
} catch (e) {
print(e.toString());
return null;
}
}
}
You have many options here, but for simplicity's sake, you could use a StreamBuilder
, and display different widgets, or pages if you please, depending on the authentication state.这里有很多选项,但为简单起见,您可以使用
StreamBuilder
并根据身份验证 state 显示不同的小部件或页面。
authStateChanges
is good here because, and I quote : authStateChanges
在这里很好,因为,我引用:
Firebase Auth enables you to subscribe in realtime to this state via a Stream.
Firebase Auth 使您能够通过 Stream 实时订阅此 state。 Once called, the stream provides an immediate event of the user's current authentication state , and then provides subsequent events whenever the authentication state changes.
一旦被调用,stream 会提供用户当前身份验证 state 的即时事件,然后在身份验证 state 更改时提供后续事件。
Emphasis by me.由我强调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.