Error: The name 'User' is defined in the libraries 'package:firebase_auth/firebase_auth.dart' and 'package:quizmaker/models/user.dart'

Issue

The Dart Analysis shows the following errors:

error: Undefined class ‘FirebaseUser’. (undefined_class at [quizmaker] lib\services\auth.dart:7)

error: The name ‘User’ is defined in the libraries ‘package:firebase_auth/firebase_auth.dart’ and ‘package:quizmaker/models/user.dart’. (ambiguous_import at [quizmaker] lib\services\auth.dart:7)

error: The name ‘User’ is defined in the libraries ‘package:firebase_auth/firebase_auth.dart’ and ‘package:quizmaker/models/user.dart’. (ambiguous_import at [quizmaker] lib\services\auth.dart:8)

error: ‘User’ isn’t a function. (invocation_of_non_function at [quizmaker] lib\services\auth.dart:8)

error: The name ‘User’ is defined in the libraries ‘package:firebase_auth/firebase_auth.dart’ and ‘package:quizmaker/models/user.dart’. (ambiguous_import at [quizmaker] lib\services\auth.dart:15)

The code is following:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:quizmaker/models/user.dart';

class AuthService {
  FirebaseAuth _auth = FirebaseAuth.instance;

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

  Future signInEmailAndPassword(String email, password) async {
    try {
      UserCredential authResult = await _auth.signInWithEmailAndPassword(
          email: email, password: password);
      User? firebaseUser = authResult.user;
      return _userFromFirebaseUser(firebaseUser);
    } catch (e) {
      print(e.toString());
    }
  }
}

Solution

import 'package:quizmaker/models/user.dart' as u;
.
.
  u.User _userFromFirebaseUser(FirebaseUser user){
    return user != null ? u.User(uid: user.uid) : null;
  }

The error is because there is a User object in the FirebaseAuth library, and also in your custom made model. You have to specify which one is which from your imports.
Import the one from your custom model as u, as in the above code, and when you use it in your function, refer to it as u.user.

Answered By – Huthaifa Muayyad

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *