I am trying to set up Firebase Authentication for a login after setting up everything correctly I am running into an error in the dart file i created.
1- Undefined class 'FirebaseUser'
2.A value of type 'User' can't be returned from the method 'registerWithEmailAndPassword' because it has a return type of 'Future
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class User {
final String uid;
User({required this.uid});
}
class AuthBase{
User _userFromFireBase (FirebaseUser user){
return User(uid: user.uid);
}
Future<void> registerWithEmailAndPassword(
String email, String password) async {
final authResult = await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
return _userFromFireBase(authResult.user);
}
}
Related
Flutter is not writing data to my firebase collection and has errors in the debug.
Steps to reproduce
https://www.youtube.com/watch?v=EA7973HI93E&list=PL4cUxeGkcC9j--TKIdkb3ISfRbJeJYQwC&index=18
Steps to reproduce the behavior:
Run the app,
register a user.
Expected behavior:
I expected that the user will be made and data to the collection added
the output
Ignoring header X-Firebase-Locale because its value was null. D/FirebaseAuth(17181): Notifying id token listeners about user ( fTnXUNwmPUajE9ybOeP3GLW0e392 ). D/FirebaseAuth(17181): Notifying auth state listeners about user ( fTnXUNwmPUajE9ybOeP3GLW0e392 ). W/DynamiteModule(17181): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found. I/DynamiteModule(17181): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0 W/ProviderInstaller(17181): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0. W/Firestore(17181): (24.4.0) [WriteStream]: (e9a00e8) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}. W/DynamiteModule(17181): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
my auth.dart code:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:my_app/models/user.dart';
import 'package:my_app/services/database.dart';
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
// create MyUser object based on User
MyUser? _userFromFirebaseUser(User? user) {
return user != null ? MyUser(uid: user.uid) : null;
}
// auth change user stream
Stream<MyUser?> get user {
return _auth.authStateChanges().map(_userFromFirebaseUser);
}
//sign in anonymously
Future signInAnon() async {
try {
UserCredential result = await _auth.signInAnonymously();
User? user = result.user;
return _userFromFirebaseUser(user);
} catch (e) {
print(e.toString());
return null;
}
}
// sign in with email and password
Future SignInWithEmailAndPassword(String email, String password) async {
try {
UserCredential result = await _auth.signInWithEmailAndPassword(
email: email, password: password);
User? user = result.user;
return _userFromFirebaseUser(user);
} catch (e) {
print(e.toString());
return null;
}
}
// register with email and password
Future registerWithEmailAndPassword(String email, String password) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
User? user = result.user;
// create new document for the user with uid
await DatabaseService(uid: user?.uid)
.updateUserData('How do you feel', 'I am mad', 'angry');
return _userFromFirebaseUser(user);
} on FirebaseAuthException catch (e) {
print(e.toString());
return null;
}
}
// sign out
Future signOut() async {
try {
return await _auth.signOut();
} catch (e) {
print(e.toString());
return null;
}
}
}
My database.dart
import 'package:cloud_firestore/cloud_firestore.dart';
class DatabaseService {
// collection reference
final String? uid;
DatabaseService({this.uid});
final CollectionReference pollearnCollection =
FirebaseFirestore.instance.collection('Pollearn');
Future updateUserData(String question, String answer, String emotion) async {
Map<String, String> data = {
'question': question,
'answer': answer,
'emotion': emotion,
};
return await pollearnCollection.doc(uid).set(data);
}
}
I already tried the following:
turn the emulator on and off.
tried different emulator
updated google play services
turnt bluetooth off
turnt internet off and on
enabled sign in with mail and password
adding
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
I hope you guys can help me. Thanks in advance!
I need the username and password that is created to be saved in the database, I have already created my database with realtime database that I am currently using to store data from a form that I create
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:async';
import 'package:firebase_database/firebase_database.dart';
abstract class BaseAuth {
Future<String> singInWithEmailAndPassword(String email, String password);
Future<String> createUserWithEmailAndPassword(String email, String password);
Future<String> currentUser();
Future<void> singOut();
}
class Auth implements BaseAuth {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final databaseRef = FirebaseDatabase.instance.ref();
#override
Future<String> singInWithEmailAndPassword(
String email, String password) async {
UserCredential user = await _firebaseAuth.signInWithEmailAndPassword(
email: email.toString(), password: password.toString());
return user.toString();
}
#override
Future<String> createUserWithEmailAndPassword(
String email, String password) async {
UserCredential user = await _firebaseAuth.createUserWithEmailAndPassword(
email: email.toString(), password: password.toString());
return user.toString();
}
#override
Future<String> currentUser() async {
User currentUser = _firebaseAuth.currentUser!;
return currentUser.toString();
}
#override
Future<void> singOut() async {
return _firebaseAuth.signOut();
}
}
This es my auth.dart,this is where i created my methods to create and login
I hope you can support me since this is part of my final project
I have a flutter firebase createUserWithEmailAndPassword function with the displayName update.
display name prints normally at the moment of fb user creation.
After signup MainPage loads with the users email and displayName. But displayName returns null value error. If I delete displayName from the MainPage - all works fine.
If I reload app, it works fine.
When I login, it works fine.
It can't pass the displayName at the moment of signup only.
Where I am wrong?
class AuthServiceProvider extends ChangeNotifier {
final auth.FirebaseAuth _firebaseAuth = auth.FirebaseAuth.instance;
final googleSingIn = GoogleSignIn();
UserModel? _userFromFirebase(auth.User? user) {
if (user == null) {
return null;
}
return UserModel(
user.displayName,
user.uid,
user.email,
);
}
Stream<UserModel?>? get user {
return _firebaseAuth.authStateChanges().map(_userFromFirebase);
}
Future<UserModel?> createUserWithEmailAndPassword(
String name,
String email,
String password,
) async {
try {
final userCred = await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
);
auth.User? firebaseUser = _firebaseAuth.currentUser;
if (firebaseUser != null) {
await firebaseUser.updateDisplayName(name);
await firebaseUser.reload();
firebaseUser = _firebaseAuth.currentUser;
}
print('FIREBASE USER IS $firebaseUser');
return _userFromFirebase(firebaseUser);
} catch (e) {
print(e.toString());
return null;
}
}
}
If your class were to extend either StatelessWidget or StatefulWidget, then all you'd have to do is to pass the data (displayName) between the screens.
This is not an answer but a suggestion:
You should try changing the ChangeNotifier to a StatefulWidget
and pass the data between screens...
You could also setup an
Authentication class that will hold all these Future methods so that
these calls can be reusable in your code. With this method, all you have to do is to call the specific function and give its required parameters.
As usually the solution is very simple if you think a little bit.
As all this is through the firebase auth, at the main page loading I just grab the firebase user with its display name that is saved in FB both for GoogleSignIn and createUserWithEmailAndPassword (required at registration)
import 'package:firebase_auth/firebase_auth.dart' as auth;
final auth.FirebaseAuth _firebaseAuth = auth.FirebaseAuth.instance;
final String firebaseUser =
_firebaseAuth.currentUser!.displayName ?? 'Unknown user';
I created the Firebase Auth class a year ago. Now After updating the Dart language and updating the firebase auth package some errors appeared.
import 'package:firebase_auth/firebase_auth.dart';
class AuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Stream<String> get onAuthStateChanged => _firebaseAuth.onAuthStateChanged.map(
(FirebaseUser user) => user.uid,
);
// Sign up Email & passowrd
Future<String> createUserWithEmailAndPassword(
String email, String password, String name) async {
final currentUser = await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
);
//updat username
var userUpdateInfo = UserUpdateInfo();
userUpdateInfo.displayName = name;
await currentUser.updateProfile(userUpdateInfo);
await currentUser.reload();
return currentUser.uid;
}
// sign in Email & password
Future<String> sinInWithEmailAndPassword(
String email, String password) async {
return (await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password))
.uid;
}
// sign out
signOut() {
return _firebaseAuth.signOut();
}
}
click here to see error
All firebase plugins for flutter since a year ago have a lot of breaking changes you should go to firebase.flutter.dev and check it
I've been trying and searching for more than two weeks The error appears in the code:
//updat username
var userUpdateInfo = UserUpdateInfo();
userUpdateInfo.displayName = name;
await currentUser.updateProfile(userUpdateInfo);
await currentUser.reload();
return currentUser.uid;
}
The method 'UserUpdateInfo' isn't defined for the type 'AuthService'
I am trying to make login and Signup features for my project but stuck in converting from Firebase old version to the new latest one. As I want to check if user signed in or not, I have added currentUser() method but then I got an error.
As I understand FirebaseUser class has replaced by AuthResult class
The main problem comes from currentUser() method: Error is the following and when I want to register, app shuts down;
(NoSuchMethodError: The getter 'uid' was called on null. Receiver: null Tried calling: uid)
This the old:
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
abstract class BaseAuth {
Future<String> signInWithEmailAndPassword(String email, String password);
Future<String> createUserWithEmailAndPassword(String email, String password);
Future<String> currentUser();
Future<void> signOut();
}
class Auth implements BaseAuth {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
#override
Future<String> signInWithEmailAndPassword(String email, String password) async {
final FirebaseUser user = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
return user?.uid;
}
#override
Future<String> createUserWithEmailAndPassword(String email, String password) async {
final FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
return user?.uid;
}
#override
Future<String> currentUser() async {
final FirebaseUser user = await _firebaseAuth.currentUser();
return user?.uid;
}
#override
Future<void> signOut() async {
return _firebaseAuth.signOut();
}
}
This is the new one:
abstract class BaseAuth{
Future<String> signInWithEmailAndPassword (String email, String password);
Future<String> createUserWithEmailAndPassword (String email, String password);
Future<String> currentUser();
}
class Auth implements BaseAuth{
#override
Future<String> signInWithEmailAndPassword (String email, String password) async {
final AuthResult user = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
return user.user.uid;
}
#override
Future<String> createUserWithEmailAndPassword (String email, String password) async {
final AuthResult user = await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
return user.user.uid;
}
#override
Future<String> currentUser() async {
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
return user.uid; //Error comes from here
}
#override
Future<void> signOut() async {
return _firebaseAuth.signOut();
}
}