Home > Software design >  Error in FirebaseAuth while using Provider
Error in FirebaseAuth while using Provider

Time:04-12

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...

CodePudding user response:

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.

  • Related