0

Here is the auth.dart

class AuthService implements AuthImplementaion {   
  final FirebaseAuth _auth = FirebaseAuth.instance;

  User _userFromFirebaseUser(FirebaseUser user) {
    return user != null ? User(uid: user.uid) : null;
  }

  Stream<User> get user {
    return _auth.onAuthStateChanged
        //.map((FirebaseUser user) => _userFromFirebaseUser(user));
        .map(_userFromFirebaseUser);
  }

  Future signInWithEmailAndPassword(String email, String password) async {
    try {
      UserCredential result = await _auth.signInWithEmailAndPassword(
          email: email, password: password);
      FirebaseUser user = result.user;
      return user;
    } catch (error) {
      print(error.toString());
      return null;
    }
  }

Future registerWithEmailAndPassword(String email, String password) async {
    try {
      UserCredential result = await _auth.createUserWithEmailAndPassword(
          email: email, password: password);
      // ignore: deprecated_member_use
      FirebaseUser user = result.user;
      return _userFromFirebaseUser(user);
    } catch (error) {
      print(error.toString());
      return null;
    }
  }
  Future signOut() async {
    try {
      return await _auth.signOut();
    } catch (error) {
      print(error.toString());
      return null;
    }
  }
}

user.dart

class User {
  final String uid;

  User({this.uid});
}

This is the error I am getting.

The name 'User' is defined in the libraries 'package:example/model/user.dart' and 'package:firebase_auth/firebase_auth.dart'. Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports.

Pintu Jat
  • 3
  • 2
  • 1
    `User` is defined in two places, and the error message implies you've imported both. Choose one. – Dave Newton Aug 25 '20 at 20:33
  • import 'package:example/model/user.dart'; import 'package:firebase_auth/firebase_auth.dart'; These are the two packages I am using. I have defined the user model to access the user wherever I want. – Pintu Jat Aug 25 '20 at 20:44
  • ... Ok. You can't import the same name twice. – Dave Newton Aug 25 '20 at 20:49
  • I know but when I am changing the name user to myUser then it shows the error -> type '(FirebaseUser) => MyUser' is not a subtype of type '(User) => MyUser' – Pintu Jat Aug 25 '20 at 21:01

1 Answers1

0

Since both packages contain the class User then you have to use the as prefix:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:your_project/user.dart' as UserModal;

Then you can do:

 User user = FirebaseAuth.instance.currentUser;
 UserModal.User user = UserModal.User("name");

Also check the following:

Undefined class 'FirebaseUser'

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134