0

I am receiving the following error that I can't figure out. I am using Flutter 2.10.

All final variables must be initialized, but 'auth' isn't. Try adding an initializer for the field.

class SignInPage extends StatelessWidget {
  const SignInPage({Key? key, required this.onSignIn}) : super(key: key);
 final AuthBase auth;

and my class is

abstract class AuthBase {
  User get currentUser;
  Future<User> signInAnonymously();
  Future<void> signOut();
}

class Auth implements AuthBase {
  final _firebaseAuth = FirebaseAuth.instance;

  @override
  User get currentUser => _firebaseAuth.currentUser!;

  @override
  Future<User> signInAnonymously() async {
    final userCredential = await _firebaseAuth.signInAnonymously();
    return userCredential.user!;
  }

  @override
  Future<void> signOut() async {
    await _firebaseAuth.signOut();
  }
}
Brian
  • 201
  • 2
  • 16

3 Answers3

2

auth is final so it must be initialized, but in your code it is not initialized. You must write something like this:

final AuthBase auth = Auth();
Qjeta
  • 33
  • 4
1

Because your auth variable is final, it has to have a value. You're not initializing it to anything, as the error message says. Either instantiate it by using final AuthBase auth = AuthBase(); in SignInPage, or add a parameter to the constructor if that is your desired effect.

Akadeax
  • 132
  • 1
  • 7
0

as the error suggest. You must initialize the final variable by

class SignInPage extends StatelessWidget {
    final AuthBase auth;
    const SignInPage({required this.onSignIn, required this.auth,Key? key, }) : super(key: key);
}
user3555371
  • 238
  • 1
  • 7