gpt4 book ai didi

Flutter:未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'id'。接收者:空尝试调用:id

转载 作者:行者123 更新时间:2023-12-03 02:55:00 29 4
gpt4 key购买 nike

我在使用 Flutter 设置使用电子邮件和密码注册时遇到问题。我让它登录新用户并保存他们的 Firebase 身份验证信息,但它不会将任何配置文件数据保存到 Firebase 存储部分。我不确定我在这里做错了什么,我不明白为什么 id 为空。一些帮助和指导将不胜感激!
这是我得到的错误

Unhandled Exception: NoSuchMethodError: The getter 'id' was called on null.
Receiver: null
Tried calling: id
这是来自 user.dart
import 'package:cloud_firestore/cloud_firestore.dart';

class User {
final String id;
final String profileName;
final String username;
final String photoUrl;
final String url;
final String email;
final String bio;
final String createdAt;

User({
this.id,
this.profileName,
this.username,
this.photoUrl,
this.url,
this.email,
this.bio,
this.createdAt,
});

factory User.fromDocument(DocumentSnapshot doc) {
return User(
id: doc.documentID,
email: doc['email'],
username: doc['username'],
photoUrl: doc['photoUrl'],
url: doc['url'],
profileName: doc['profileName'],
bio: doc['bio'],
createdAt: doc['createdAt'],
);
}
}
这是来自 Signup.dart
该错误指向 usersReference.document 行。
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:buddiesgram/models/user.dart';
import 'package:buddiesgram/pages/HomePage.dart';
import 'package:shared_preferences/shared_preferences.dart';


class SignupPage extends StatefulWidget {

static final String id = 'signup_page';
final DateTime timestamp = DateTime.now();
User currentUser;

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

class _SignupPageState extends State<SignupPage> {
final FirebaseAuth auth = FirebaseAuth.instance;
final _formKey = GlobalKey<FormState>();
String username, email, password;
SharedPreferences preferences;

checkIfSignedIn() async {
auth.onAuthStateChanged.listen((user) {

if (user != null) {
Navigator.push(context, MaterialPageRoute(builder: (context) => HomePage()));
}
});

@override
void initState() {
super.initState();
this.checkIfSignedIn();
}
}

saveUserInfoToFireStore() async {

preferences = await SharedPreferences.getInstance();
DocumentSnapshot documentSnapshot = await usersReference.document(currentUser.id).get();

if(!documentSnapshot.exists) {
usersReference.document(currentUser.id).setData({
"id": currentUser.id,
"profileName": currentUser.profileName,
"username": currentUser.username,
"photoUrl": currentUser.photoUrl,
"email": currentUser.email,
"bio": "",
"timestamp": timestamp,
"talkingTo": null,
});

//Write data to local
//currentUser = currentUser as User;
//await preferences.setString("id", currentUser.id);
//await preferences.setString("profileName", currentUser.profileName);
//await preferences.setString("photoUrl", currentUser.photoUrl);

await followersReference.document(currentUser.id).collection("userFollowers").document(currentUser.id).setData({});

documentSnapshot = await usersReference.document(currentUser.id).get();
}

currentUser = User.fromDocument(documentSnapshot);
}

signUp() async {
if(_formKey.currentState.validate()) {
_formKey.currentState.save();

try{
AuthResult authResult = await auth.createUserWithEmailAndPassword(email: email, password: password);
FirebaseUser signedInUser = authResult.user;
if(signedInUser != null) {
saveUserInfoToFireStore();
}
Navigator.push(context, MaterialPageRoute(builder: (context) => HomePage()));
}
catch(e) {
print(e);
}
}
}
这是来自 Timeline.dart
该错误指向这两种方法的两条 QuerySnapshot 行。
retrieveTimeLine() async {
QuerySnapshot querySnapshot = await timelineReference.document(currentUser.id)
.collection("timelinePosts").orderBy("timestamp", descending: true).getDocuments();

List<Post> allPosts = querySnapshot.documents.map((document) => Post.fromDocument(document)).toList();

setState(() {
this.posts = allPosts;
});
}

retrieveFollowings() async {
QuerySnapshot querySnapshot = await followingReference.document(currentUser.id)
.collection("userFollowing").getDocuments();

setState(() {
followingsList = querySnapshot.documents.map((document) => document.documentID).toList();
});
}
如果我遗漏了什么,请告诉我,这可能会有所帮助。

最佳答案

currentUser为空,因为您没有初始化该类。例如:

  saveUserInfoToFireStore() async {
currentUser = User(); //initialize
preferences = await SharedPreferences.getInstance();
DocumentSnapshot documentSnapshot = await usersReference.document(currentUser.id).get();
当然上面的代码还是不行,因为 id等于空。如果数据库中的文档 id 等于用户 uid,则执行以下操作:
User loggedInUser;

saveUserInfoToFireStore() async {
var user = FirebaseAuth.instance.currentUser();
loggedInUser = User(id : user.uid);
preferences = await SharedPreferences.getInstance();
DocumentSnapshot documentSnapshot = await usersReference.document(loggedInUser.id).get();
所以在这里你得到了 uid来自 Firebase 身份验证的用户,然后由于您使用可选的命名参数,您可以初始化 Userid 一起上课属性并在 document() 中使用它方法。

关于Flutter:未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'id'。接收者:空尝试调用:id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62895099/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com