Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase authentication error: Undefined name 'auth'

I would like to acces my firestore databse, by using the UID of each user, like so:

return new StreamBuilder(
    stream: Firestore.instance.collection('users').document(uid).snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return new Text("Loading");
      }
      var userDocument = snapshot.data;

And for getting the UID:

final FirebaseUser user = auth.currentUser().then((FirebaseUser user) {
  final userid = user.uid;
});

This but I keep getting the error

Undefined name 'auth'.
like image 506
Oliver Olsen Avatar asked May 15 '26 03:05

Oliver Olsen


2 Answers

To use FirebaseAuth in flutter, you must get a reference to such instance. Add this line at the top of your widget/file whenever you need to use Firebase Authentication:

import 'package:firebase_auth/firebase_auth.dart';

FirebaseAuth auth = FirebaseAuth.instance;

Now you can use stuff like auth.currentUser().then(...) like you are doing correctly.

like image 147
George Avatar answered May 16 '26 20:05

George


Undefined name 'FirebaseAuth', and not importing library firebase_auth

like image 30
Milan Gopani Avatar answered May 16 '26 22:05

Milan Gopani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!