Home > Blockchain >  Undefined name 'FirebaseAuth'
Undefined name 'FirebaseAuth'

Time:10-06

The FirebaseAuth was working fine for me a few hours back but now I am receiving an error as : Undefinded name 'FirebaseAuth' .I am unable to resolve it. Don't know what is the real issue. Would be a great help for me if anybody could step in and provide me a or more solutions.

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:jiwdopani/UserLogIn/MenuUse.dart';
import 'package:jiwdopani/services/error_handler.dart';
import 'package:jiwdopani/UserLogIn/login_page.dart';


class AuthService {

  handleAuth() {
    return StreamBuilder(

        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (BuildContext context, snapshot) {
          if (snapshot.hasData) {
            return UserMenu();
          } else
            return LoginPage();
        });
  }

  //Sign out
  signOut() async {
    try {
      return await FirebaseAuth.instance.signOut();
    }
    catch(e){
      print(e.toString());
      return null;
    }

  }


  //Sign In
  signIn(String email, String password, context) {
    FirebaseAuth.instance
        .signInWithEmailAndPassword(email: email, password: password)
        .then((val) {
      UserMenu();
    }).catchError((e) {
      ErrorHandler().errorDialog(context, e);
    });
  }

  //Signup a new user
  signUp(String email, String password) {
    return FirebaseAuth.instance


        .createUserWithEmailAndPassword(email: email, password: password);

  }
//Reset Password
  resetPasswordLink(String email) {
    FirebaseAuth.instance.sendPasswordResetEmail(email: email);
  }

CodePudding user response:

I fixed some of your code by adding types to the functions:

class AuthService {
  Widget handleAuth() {
    return StreamBuilder(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (BuildContext context, snapshot) {
          if (snapshot.hasData) {
            return Container(); //change it back to your widget
          } else
            return Container();//change it back to your widget
        });
  }

  //Sign out
  void signOut() async {
    try {
      return await FirebaseAuth.instance.signOut();
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  //Sign In
  void signIn(String email, String password, BuildContext context) {
    FirebaseAuth.instance
        .signInWithEmailAndPassword(email: email, password: password)
        .then((val) {
    return  Container();
    });
  }

  //Signup a new user
  Future<UserCredential> signUp(String email, String password) async {
    return await FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: email, password: password);
  }

//Reset Password
  void resetPasswordLink(String email) {
    FirebaseAuth.instance.sendPasswordResetEmail(email: email);
  }
}

Anyway, I think it's best not to have Widgets or UI in Auth Class, it is always better to separate UI from logic.

  • Related