我對 Dart 非常陌生,并且一般都在編碼。在觀看 YouTube 上的教程后,我生成了此代碼。在大多數情況下,我已經能夠自己解決大部分問題,但我無法弄清楚我最近的錯誤。我在 firestorm 中成功創建了記錄,但我無法更新它,因為我無法獲取 Doc ID。
這是我的代碼:-
import 'package:flutter/material.dart';
import 'package:lms_definer/controllers/profileController.dart';
import 'package:lms_definer/controllers/textController.dart';
import 'package:get/get.dart';
import 'package:lms_definer/model/profileModel.dart';
import 'package:lms_definer/constants/constants.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '../helper/firestore_db.dart';
class EditProfile extends StatelessWidget {
const EditProfile({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var _textController = ProfileEdit();
return Scaffold(
body: GetX<ProfileController>(
init: Get.put<ProfileController>(ProfileController()),
builder: (ProfileController profileController) {
return Container(
child: ListView.builder(
itemCount: profileController.profiles.length,
itemBuilder: (BuildContext context, int i) {
final _profileModel = profileController.profiles[i];
setTextEditControllerValue(_textController, _profileModel);
return SafeArea(
child: Container(
padding: EdgeInsets.all(20),
child: Form(
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(
labelText: 'First Name',
border: OutlineInputBorder(
borderSide: BorderSide()),
),
controller: _textController.fNameController
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'First Name',
border: OutlineInputBorder(
borderSide: BorderSide()),
),
controller: _textController.lNameController,
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Address',
border: OutlineInputBorder(
borderSide: BorderSide()),
),
controller: _textController.adressController,
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Phone Numbaer',
border: OutlineInputBorder(
borderSide: BorderSide()),
),
controller: _textController.phoneController,
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'School Name',
border: OutlineInputBorder(
borderSide: BorderSide()),
),
controller: _textController.sclNameController,
),
SizedBox(
height: 10,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Student Class',
border: OutlineInputBorder(
borderSide: BorderSide()),
),
controller: _textController.stdClassController,
),
SizedBox(
height: 10,
),
ElevatedButton(onPressed: () async {
ProfileModel profileModel = profileModelVal(_textController);
//await FirestoreDb.updateProfile(profileModel, documentId);
}, child: Text('Update'))
],
),
),
),
);
}),
);
},
));
}
ProfileModel profileModelVal(ProfileEdit _textController) {
final profileModel = ProfileModel(
firstName:
_textController.fNameController.text.trim(),
lastName:
_textController.lNameController.text.trim(),
parentName: _textController
.fatherNameController.text
.trim(),
phoneNumber:
_textController.phoneController.text.trim(),
address:
_textController.adressController.text.trim(),
schoolName:
_textController.sclNameController.text.trim(),
stdClass: _textController.stdClassController.text
.trim());
return profileModel;
}
void setTextEditControllerValue(ProfileEdit _textController,
ProfileModel _profileModel) {
_textController.fNameController.value =
TextEditingValue(text: _profileModel.firstName);
_textController.lNameController.value =
TextEditingValue(text: _profileModel.lastName);
_textController.fatherNameController.value =
TextEditingValue(text: _profileModel.parentName);
_textController.adressController.value =
TextEditingValue(text: _profileModel.address);
_textController.phoneController.value =
TextEditingValue(text: _profileModel.phoneNumber);
_textController.sclNameController.value =
TextEditingValue(text: _profileModel.schoolName);
_textController.stdClassController.value =
TextEditingValue(text: _profileModel.stdClass);
}
void clearTextController(ProfileEdit _textController) {
_textController.fNameController.clear();
_textController.lNameController.clear();
_textController.fatherNameController.clear();
_textController.adressController.clear();
_textController.phoneController.clear();
_textController.sclNameController.clear();
_textController.stdClassController.clear();
}
}
我的資料庫檔案:-
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:lms_definer/constants/constants.dart';
import 'package:lms_definer/model/profileModel.dart';
class FirestoreDb {
static createProfile(ProfileModel profilemodel) async {
await firebaseFirestore.collection('users').doc(auth.currentUser!.uid)
.collection('profile')
.add({
'firstName': profilemodel.firstName,
'lastName': profilemodel.lastName,
'parentName': profilemodel.parentName,
'phoneNumber': profilemodel.phoneNumber,
'address': profilemodel.address,
'schoolName': profilemodel.schoolName,
'stdClass': profilemodel.stdClass,
'createdOn': Timestamp.now(),
'isDone': true
});
}
static Stream<List<ProfileModel>> profileStream() {
return firebaseFirestore
.collection('users')
.doc(auth.currentUser!.uid)
.collection('profile')
.snapshots()
.map((QuerySnapshot query) {
List<ProfileModel> profiles = [];
for (var profile in query.docs) {
final profileModel =
ProfileModel.fromDocmentSnapshot(documentSnapshot: profile);
profiles.add(profileModel);
}
return profiles;
});
}
static updateProfile(ProfileModel profilemodel, documentId ) {
firebaseFirestore
.collection('users')
.doc(auth.currentUser!.uid)
.collection('profile')
.doc(documentId)
.update(
{
'firstName': profilemodel.firstName,
'lastName': profilemodel.lastName,
'parentName': profilemodel.parentName,
'phoneNumber': profilemodel.phoneNumber,
'address': profilemodel.address,
'schoolName': profilemodel.schoolName,
'stdClass': profilemodel.stdClass,
'createdOn': Timestamp.now(),
}
);
}
static deleteProfile(String documentID) {
firebaseFirestore.collection('users').doc(auth.currentUser!.uid).collection(
'profile').doc(documentID).delete();
}}

我需要 doc Id 來更新檔案。請幫忙。謝謝
uj5u.com熱心網友回復:
static updateProfile(ProfileModel profilemodel, documentId ) {
firebaseFirestore
.collection('users')
.doc(auth.currentUser!.uid)
.collection('profile')
.get()
.then((QuerySnapshot querySnapshot) {
final docId = queryShapchot.docs.first.id;
firebaseFirestore
.collection('users')
.doc(auth.currentUser!.uid)
.collection('profile')
.doc(docId)
.update({
// Add data here
})
});
}
這應該為您解決問題。
uj5u.com熱心網友回復:
一旦用戶登錄(我假設 EditProfile 頁面僅對登錄用戶可見),您應該將 userId 存盤在某處。這可以使用flutter_secure_storage或使用像FirebaseAuth這樣的身份驗證庫來完成。
然后當你想更新用戶的檔案時,你可以獲取之前存盤的 userId 來更新檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/453345.html
上一篇:NoSuchMethodError:在null上呼叫了getter'docs'。撲
下一篇:在firebase中獲取多個資料
