使用提供商时Firebaseauth的错误

发布于 2025-01-20 13:53:15 字数 2523 浏览 2 评论 0原文

我想用firebase_auth创建一个输入页面。我可以成功登录,但无法登录。当我尝试登录应用程序时,没有给出任何错误,但不会传递主页。仍然在登录页面中。现在,当我重新启动调试时,它会通过主页。你能看到我的代码并帮助我吗?

这是我的身份服务代码

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:library_app/models/person.dart';

class FirebaseAuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Person? _createPerson(User? user) {
    return user == null ? null : Person.fromFirebaseUser(user);
  }

  Stream<Person?> get statusFollower {
    return _auth.authStateChanges().map(_createPerson);
  }

  void createUserEmailAndPassword({
    required String email,
    required String password,
  }) async {
    try {
      var _userCredential = await _auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
      _createPerson(_userCredential.user);
    } catch (e) {
      debugPrint(e.toString());
    }
  }

  loginUserEmailAndPassword({
    required String email,
    required String password,
  }) async {
    try {
      var _userCredential = await _auth.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
      _createPerson(_userCredential.user);
    } catch (e) {
      debugPrint(e.toString());
    }
  }

  void signOut() async {
    await _auth.signOut();
  }
}

,这是我的方向代码

import 'package:flutter/cupertino.dart';
import 'package:library_app/models/person.dart';
import 'package:library_app/pages/error_page.dart';
import 'package:library_app/pages/loading_page.dart';
import 'package:library_app/pages/main_page.dart';
import 'package:library_app/pages/sign_in_page.dart';
import 'package:library_app/services/firebase_auth_service.dart';
import 'package:provider/provider.dart';

class OrientationSystem extends StatelessWidget {
  const OrientationSystem({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var _authService = Provider.of<FirebaseAuthService>(context, listen: false);
    return StreamBuilder<Person?>(
      stream: _authService.statusFollower,
      builder: (context, stream) {
        if (stream.connectionState == ConnectionState.waiting) {
          return const LoadingPage();
        }
        if (stream.hasData) {
          return const MainPage();
        }
        if (!stream.hasData) {
          return const SigInPage();
        } else {
          return const ErrorPage();
        }
      },
    );
  }
}

我该怎么办? 请帮忙...

I want to create an entering page with firebase_auth. I can sign in successfully but I can't log in. When I try to log in application doesn't give any error but it isn't pass the main page. Still it is in log in page. When I restart the debugging now it pass the main page. Can you see my code and help me?

Here is my auth service code

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:library_app/models/person.dart';

class FirebaseAuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Person? _createPerson(User? user) {
    return user == null ? null : Person.fromFirebaseUser(user);
  }

  Stream<Person?> get statusFollower {
    return _auth.authStateChanges().map(_createPerson);
  }

  void createUserEmailAndPassword({
    required String email,
    required String password,
  }) async {
    try {
      var _userCredential = await _auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
      _createPerson(_userCredential.user);
    } catch (e) {
      debugPrint(e.toString());
    }
  }

  loginUserEmailAndPassword({
    required String email,
    required String password,
  }) async {
    try {
      var _userCredential = await _auth.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
      _createPerson(_userCredential.user);
    } catch (e) {
      debugPrint(e.toString());
    }
  }

  void signOut() async {
    await _auth.signOut();
  }
}

And here is my orientation code

import 'package:flutter/cupertino.dart';
import 'package:library_app/models/person.dart';
import 'package:library_app/pages/error_page.dart';
import 'package:library_app/pages/loading_page.dart';
import 'package:library_app/pages/main_page.dart';
import 'package:library_app/pages/sign_in_page.dart';
import 'package:library_app/services/firebase_auth_service.dart';
import 'package:provider/provider.dart';

class OrientationSystem extends StatelessWidget {
  const OrientationSystem({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var _authService = Provider.of<FirebaseAuthService>(context, listen: false);
    return StreamBuilder<Person?>(
      stream: _authService.statusFollower,
      builder: (context, stream) {
        if (stream.connectionState == ConnectionState.waiting) {
          return const LoadingPage();
        }
        if (stream.hasData) {
          return const MainPage();
        }
        if (!stream.hasData) {
          return const SigInPage();
        } else {
          return const ErrorPage();
        }
      },
    );
  }
}

what must I do?
please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2025-01-27 13:53:15

您没有在 firbaseauthservice 结构中延伸或与changeNotifier类一起使用。此外,您需要在执行任务后或更新Firebaseauthservice类中的数据后 notifyListener(),以便您的其他类可以收听更新的值。
但是,请将您的问题更新为“使用提供商时Firebaseauth中的错误”之类的问题。使您的问题标题尽可能多地相关,否则您可能会被系统禁止。这是一个建议。

You did not extend or used with ChangeNotifier class in the FirebaseAuthService construction. Also, you need to notifyListener() after doing the tasks or after updating data inside your FirebaseAuthService class so that your other classes may listen to the updated values.
However, please update your question to something like "Error in FirebaseAuth while using Provider". Make your question titles as much relevant as you can otherwise you might get ban by the system. This is a suggestion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文