简体   繁体   English

Flutter:如何设置 Stream 和提供程序

[英]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.

相关问题 使用提供程序进行 DI 时,如何根据 Firebase Auth 流修改 Flutter Firebase Stream 侦听器? - How to modify Flutter Firebase Stream listener based on Firebase Auth stream when using Provider for DI? Flutter/Firestore/Provider - 瞬间显示错误,然后显示 stream,如何在启动时加载 stream 值? - Flutter/Firestore/Provider - Error shown for split second then stream displayed, how can I load stream values on startup? 如何在Flutter Web项目中设置Firebase功能 - How to set up firebase function in flutter web project 如何在 Flutter Modular 中设置 RouteGuard 以进行 Firebase 身份验证 - how to set up RouteGuard in Flutter Modular for Firebase authentication 如何将 Stream 传递给 GroupedListView Flutter - How to pass Stream to GroupedListView Flutter 如何将 Stream 转换为 Flutter 中的 Listenable? - How to convert a Stream to a Listenable in Flutter? Flutter - 提供者 - 如何根据提供者的值更改 UI? - Flutter - Provider - How to change UI based on value from Provider? 如何在 Flutter 中更改 StreamProvider 中的 stream - How to change the stream in StreamProvider in Flutter 如何从 Hero 小部件调用提供程序 - Flutter - How to call a provider from a Hero widget - Flutter 如何使 flutter dart 中的流与提供程序一起使用 - How To Make Streams in flutter dart work with provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM