Flutter Firebase身份验证错误

时间:2020-08-29 08:28:47

标签: flutter firebase-authentication

我正在摸索我的loginPage.dart。我升级了1.12之前的项目。 这似乎是迁移的最后一部分

我在代码块中将错误加粗了

错误是:

“用户”不是功能 尝试更正名称以匹配现有功能,或定义名为“用户”的方法或功能

错误:

未为类型“ UserCredential”定义吸气剂“ uid” 尝试导入定义“ did”的库,更正其中一个导入中的所有内容的名称

错误:

库“ package:firebase_auth / firebase_auth.dar”中未定义“用户”名称 尝试更正名称以匹配现有功能,或定义名为“用户”的方法或功能

希望有人能帮助我吗?

From firebase_core: ^0.4.0+1 to firebase_core: ^0.5.0
From firebase_auth: ^0.11.1+3 to firebase_auth: ^0.18.0+1
From cloud_firestore: ^0.12.7+1 to cloud_firestore: ^0.14.0+2
From firebase_storage: ^3.0.4 to firebase_storage: ^4.0.0

loginPage.dart

import 'package:firebase_auth/firebase_auth.dart';

import '../Models/appConstants.dart';
import '../Models/userObjects.dart';
import './guestHomePage.dart';
import './signUpPage.dart';

class LoginPage extends StatefulWidget {
  static final String routeName = '/loginPageRoute';

  LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  TextEditingController _emailController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();

  void _signUp() {
    if (_formKey.currentState.validate()) {
      String email = _emailController.text;
      String password = _passwordController.text;
      **AppConstants.currentUser = User();**
      AppConstants.currentUser.email = email;
      AppConstants.currentUser.password = password;
      Navigator.pushNamed(context, SignUpPage.routeName);
    }
  }

  void _login() {
    if (_formKey.currentState.validate()) {
      String email = _emailController.text;
      String password = _passwordController.text;
      FirebaseAuth.instance
          .signInWithEmailAndPassword(
        email: email,
        password: password,
      )
          .then((firebaseUser) {
        **String userID = firebaseUser.uid;**
        **AppConstants.currentUser = User(id: userID);**
        AppConstants.currentUser
            .getPersonalInfoFromFirestore()
            .whenComplete(() {
          Navigator.pushNamed(context, GuestHomePage.routeName);
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Center(
          child: Padding(
            padding: const EdgeInsets.fromLTRB(50, 100, 50, 0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Text(
                  'Welcome to ${AppConstants.appName}!',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 30.0,
                  ),
                  textAlign: TextAlign.center,
                ),
                Form(
                  key: _formKey,
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.only(top: 35.0),
                        child: TextFormField(
                          decoration: InputDecoration(labelText: 'Email'),
                          style: TextStyle(
                            fontSize: 25.0,
                          ),
                          validator: (text) {
                            if (!text.contains('@')) {
                              return 'Please enter a valid email';
                            }
                            return null;
                          },
                          controller: _emailController,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 25.0),
                        child: TextFormField(
                          decoration: InputDecoration(labelText: 'Password'),
                          style: TextStyle(
                            fontSize: 25.0,
                          ),
                          obscureText: true,
                          validator: (text) {
                            if (text.length < 6) {
                              return 'Password must be at least 6 characters';
                            }
                            return null;
                          },
                          controller: _passwordController,
                        ),
                      )
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 30.0),
                  child: MaterialButton(
                    onPressed: () {
                      _login();
                    },
                    child: Text(
                      'Login',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 25.0,
                      ),
                    ),
                    color: Colors.blue,
                    height: MediaQuery.of(context).size.height / 12,
                    minWidth: double.infinity,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 30.0),
                  child: MaterialButton(
                    onPressed: () {
                      _signUp();
                    },
                    child: Text(
                      'Sign Up',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 25.0,
                      ),
                    ),
                    color: Colors.grey,
                    height: MediaQuery.of(context).size.height / 12,
                    minWidth: double.infinity,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
} ```

2 个答案:

答案 0 :(得分:1)

FlutterFire库最近已升级,这可能是您的问题出处。

  1. FirebaseUser类现在简称为User,您还应该查看是否将鼠标悬停在IDE中的错误上。

  2. 要获取当前用户,您不再执行await FirebaseAuth.instance.currentUser(),而是执行FirebaseAuth.instance.currentUser(因此,没有await())。

  3. signInWithEmailAndPassword method解析为UserCredential对象。要获取UID,请执行以下操作:

    .then((credentials) {
        String userID = credentials.user.uid;
    

答案 1 :(得分:0)

您错过了在.currentUser()之前添加.uid的问题,而jsut会因为其他所有问题而这样做

但是我希望这样可以简化您的代码

final FirebaseAuth auth = FirebaseAuth.instance;

void inputData() async {
    final FirebaseUser user = await auth.currentUser();
    final uid = user.uid;
    // here you write the codes to input the data into firestore
  }