代码运行正常,但是当我更新依赖项时,开始出现以下错误:
这是代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:za/pages/signup.dart';
import 'home.dart';
class Login extends StatefulWidget {
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
final GoogleSignIn googleSignIn = new GoogleSignIn();
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
final _formKey = GlobalKey<FormState>();
TextEditingController _emailTextController = TextEditingController();
TextEditingController _passwordTextController = TextEditingController();
SharedPreferences preferences;
bool loading = false;
bool islogedin = false;
bool hidepass = true;
@override
void initState() {
super.initState();
isSignedIn();
}
void isSignedIn() async {
setState(() {
loading = true;
});
await firebaseAuth.currentUser().then((user){
if (user!=null){
setState(() => islogedin= true);
}
});
if (islogedin == true) {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => HomePage()));
}
setState(() {
loading = false;
});
}
Future handleSignIn() async {
preferences = await SharedPreferences.getInstance();
setState(() {
loading = true;
});
final GoogleSignInAccount googleUser = await googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
User firebaseUser =
(await firebaseAuth.signInWithCredential(credential)).user;
if (firebaseUser != null) {
final QuerySnapshot result = await Firestore.instance
.collection("users")
.where("id", isEqualTo: firebaseUser.uid)
.get();
final List<DocumentSnapshot> documents = result.docs;
if (documents.length == 0) {
// insert the user to our collection
FirebaseFirestore.instance
.collection("users")
.doc(firebaseUser.uid)
.set({
"id": firebaseUser.uid,
"username": firebaseUser.displayName,
"profilePicture": firebaseUser.photoURL
});
await preferences.setString("id", firebaseUser.uid);
await preferences.setString("username", firebaseUser.displayName);
await preferences.setString("photoUrl", firebaseUser.displayName);
} else {
await preferences.setString("id", documents[0].data['id']);
await preferences.setString("username", documents[0].data['username']);
await preferences.setString("photoUrl", documents[0].data['photoUrl']);
}
Fluttertoast.showToast(msg: "Login was successful");
setState(() {
loading = false;
});
} else {
Fluttertoast.showToast(msg: "Login failed");
}
}
答案 0 :(得分:3)
DocumentSnapshot
没有[]
运算符,正如错误明确指出的那样。它具有data
字段。替换此代码:
await preferences.setString("id", documents[0]['id']);
await preferences.setString("username", documents[0]['username']);
await preferences.setString("photoUrl", documents[0]['photoUrl']);
与此:
await preferences.setString("id", documents[0].data['id']);
await preferences.setString("username", documents[0].data['username']);
await preferences.setString("photoUrl", documents[0].data['photoUrl']);
/// Returns the current [User] if they are currently signed-in, or `null` if
/// not.
///
/// You should not use this getter to determine the users current state,
/// instead use [authStateChanges], [idTokenChanges] or [userChanges] to
/// subscribe to updates.
User get currentUser {
if (_delegate.currentUser != null) {
return User._(this, _delegate.currentUser);
}
return null;
}
答案 1 :(得分:0)
尝试await preferences.setString("username", documents[0].data()['username']);
顺便说一句,您是否更新了Firebase软件包?如果是,请点击此链接以检查更新的更改https://firebase.flutter.dev/docs/migration/
答案 2 :(得分:0)
固定1,我更改了
await preferences.setString("id", documents[0]['id']);
await preferences.setString("username", documents[0]['username']);
await preferences.setString("photoUrl", documents[0]['photoUrl']);
到
await preferences.setString("id", documents[0].data()['id']);
await preferences.setString("username", documents[0].data()['username']);
await preferences.setString("photoUrl", documents[0].data()['photoUrl']);
显然,现在可以通过 data()方法来通过 data 获取器获取快照数据。
我只剩下错误 在
上await firebaseAuth.currentUser().then((user){
if (user!=null){
setState(() => islogedin= true);
}
根据文档(https://firebase.flutter.dev/docs/migration/),现在可以通过currentUser getter同步通过currentUser()访问当前用户。