简体   繁体   English

Flutter Riverpod 1.0:Firebase AuthService 对象使用什么提供程序?

[英]Flutter Riverpod 1.0: What provider to use for Firebase AuthService object?

I want to use Firebase Phone authentication for an Flutter app.我想对 Flutter 应用程序使用 Firebase 电话身份验证。 I have an AuthService class and I need to expose getter codeSent to change a UI.我有一个 AuthService 类,我需要公开 getter codeSent 来更改 UI。 Third party examples from official Riverpod's site are using other packages and not very clear.来自官方 Riverpod 站点的第三方示例正在使用其他软件包并且不是很清楚。 So how to use Riverod in this case?那么在这种情况下如何使用 Riverod 呢?

import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  String _verificationId = '';
  int? _resendToken;
  bool _codeSent = false;

  Stream<User?> get user {
    return _auth.authStateChanges();
  }

  get currentUser {
    return _auth.currentUser;
  }

  Future verifyPhoneNumber(String phoneNumber) async {
    await _auth.verifyPhoneNumber(
        verificationCompleted: (PhoneAuthCredential credential) async {
          await _auth.signInWithCredential(credential);
        },
        verificationFailed: (FirebaseAuthException e) {
          if (e.code == 'invalid-phone-number') {
            print('The provided phone number is not valid.');
          }
        },
        codeSent: (String verificationId, int? resendToken) {
          _verificationId = verificationId;
          _resendToken = resendToken;
          _codeSent = true;
        },
        codeAutoRetrievalTimeout: (String verificationId) {
          _verificationId = verificationId;
        });
  }

  Future signWithOtp(String otp) async {
    PhoneAuthCredential credential = PhoneAuthProvider.credential(
        verificationId: _verificationId, smsCode: otp);
    await _auth.signInWithCredential(credential);
  }

  Future signOut() async {
    try {
      return await _auth.signOut();
    } catch (error) {
      print(error.toString());
      return null;
    }
  }
}

The problem was solved with ChangeNotifierProvider.问题已通过 ChangeNotifierProvider 解决。

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:aptekamall/service/auth.dart';

final authProvider = ChangeNotifierProvider<AuthService>((ref) {
  return AuthService();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 登录Provider AuthService - Flutter login Provider AuthService Flutter Riverpod Firebase currentUser Provider 未更新 - Flutter Riverpod Firebase currentUser Provider not updated Flutter Riverpod 和 Firebase - Flutter Riverpod and Firebase Flutter Riverpod:使用 Riverpod 从 Firebase 获取数据时出现问题 - Flutter Riverpod : Problems fetching data from Firebase using Riverpod 仅当代码在提供程序中时,Riverpod 提供程序列表才会从 firebase 更新 - Riverpod provider list updates from firebase only if the code is in the provider 与提供商 flutter 的身份验证 Firebase - authentification Firebase with Provider flutter Flutter:提供者和 Firebase | 来自 Firestore 数据(映射)的 Object 列表从提供者返回 null - Flutter: Provider and Firebase | List of Object from Firestore Data (mapped) returns null from Provider Firebase Flutter Riverpod:在使用 Riverpod 跨模型映射值时,如何从 Firebase 获取 documentID? 包含代码 - Firebase Flutter Riverpod: How do I get the documentID from Firebase when using Riverpod to map values across a model? Code Included Flutter Firebase authStateChanges() 与 Provider 和 NullSafety On - Flutter Firebase authStateChanges() with Provider & NullSafety On 使用 Flutter,如何在单独的登录页面小部件中显示在 AuthService 类中捕获的 Firebase Auth 错误消息? - With Flutter, how can I display Firebase Auth error messages caught within an AuthService class within a separate log in page widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM