简体   繁体   中英

Getter isn't defined for the class. What am i doing wrong here?

I'm following a tutorial building my first login system with Flutter. I stumbled upon a getter error I can't resolve.

Before I refactored the code to move the auth parts into their own file, everything was working fine.

Here's the problematic code:

abstract class BaseAuth {
  Future<String> signInWithEmailAndPassword(String email, String password);
}

class Auth implements BaseAuth {
  Future<String> signInWithEmailAndPassword(String email, String password) async {
     FirebaseUser user = (await
      FirebaseAuth.instance.signInWithEmailAndPassword(
        email: email,
        password: password
      )).user;
    return user.uid;
  }
}

Here's how I try to use it:

class LoginPage extends StatefulWidget {
  LoginPage({this.auth});
  final BaseAuth auth;

  @override
  State<StatefulWidget> createState() => _LoginPageState();
}

enum FormType {
  login,
  register
}

...

class _LoginPageState extends State<StatefulWidget> {
  final formKey = GlobalKey<FormState>();

  String _email;
  String _password;
  FormType _formType = FormType.login;

  void validateAndSubmit() async {
    if (validateAndSave()) {
      try {
        if (_formType == FormType.login) {
          String userID = await widget.auth.singInWithEmailAndPassword(_email,_password);
          print('Signed in as $userID');
        } ...
      } catch(e) {
        print('Error: $e');
      }
    }
  }
...
}

This is the error message I receive: "The getter 'auth' isn't defined for the class StatefulWidget".

The code works just fine in the tutorial.

I keep looking for differences between our codebases but there's nothing that seems out of place to me. Maybe someone can help an improver out?

class _LoginPageState extends State<StatefulWidget> {

应该

class _LoginPageState extends State<LoginPage> {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM