I take data from Firestore by user id. I need to display data in a TextFormField and be able to update it immediately on button click. There was a problem, because I'm not building a list, but I need to display data for only one id. How do I correctly access the firestore elements, to the id, I need to get these lines, only this code does not work for me var noteInfo = snapshot.data!.docs.data()!; String docID = snapshot.data!.docs.id; String title = noteInfo['title']; You need to refer to elements only without [index] I will sharpen that I do not have a list. Does anyone know how to contact them correctly? I will be grateful for help
firestore_repository
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final CollectionReference _mainCollection = _firestore.collection('users');
class Database {
static String? userUid;
static Future<void> addItem({
required String bisName,
required String bisAddress,
required String contactName,
required String contactEmail,
required String phoneNumber,
required String progName,
// required String progYears,
}) async {
DocumentReference documentReference =
_mainCollection.doc(userUid).collection('info').doc();
Map<String, dynamic> data = <String, dynamic>{
'bisName': bisName,
'bisAddress': bisAddress,
'contactName': contactName,
'contactEmail': contactEmail,
'phoneNumber': phoneNumber,
'progName': progName,
// 'progYears': progYears,
};
await documentReference
.set(data)
.whenComplete(() => print('User info added to the database'))
.catchError((e) => print(e));
}
static Future<void> updateItem({
required String bisName,
required String bisAddress,
required String contactName,
required String contactEmail,
required String phoneNumber,
required String progName,
required String docId,
}) async {
DocumentReference documentReference =
_mainCollection.doc(userUid).collection('info').doc(docId);
Map<String, dynamic> data = <String, dynamic>{
'bisName': bisName,
'bisAddress': bisAddress,
'contactName': contactName,
'contactEmail': contactEmail,
'phoneNumber': phoneNumber,
'progName': progName,
};
await documentReference
.update(data)
.whenComplete(() => print('User info updated to the database'))
.catchError((e) => print(e));
}
static Stream<QuerySnapshot> readItems() {
CollectionReference usersInfoCollection =
_mainCollection.doc(userUid).collection('info');
return usersInfoCollection.snapshots();
}
static Future<void> deleteItem({
required String docId,
}) async {
DocumentReference documentReference =
_mainCollection.doc(userUid).collection('info').doc(docId);
await documentReference
.delete()
.whenComplete(() => print('User info deleted to the database'))
.catchError((e) => print(e));
}
}
profile_screen
class ProfileScreen extends StatefulWidget {
@override
State<ProfileScreen> createState() => _ProfileScreen();
}
class _ProfileScreen extends State<ProfileScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
child: Padding(
padding: const EdgeInsets.all(50),
child: StreamBuilder<QuerySnapshot>(
stream: Database.readItems(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
} else if (snapshot.hasData || snapshot.data != null) {
return _userInfo(snapshot);
}
}
),
),
)));
}
Widget _userInfo(AsyncSnapshot<QuerySnapshot> snapshot) {
var userInfo = snapshot.data!.docs;
// String docId = snapshot.data.docs;
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Form(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: MediaQuery.of(context).size.width * 0.2,
child: TextFormField(
decoration: InputDecoration(
labelText: "Business Name".toUpperCase(),
labelStyle: TextStyle(
color: primaryColor, fontWeight: FontWeight.w700),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: secondaryColor,
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: primaryColor,
)),
),
),
),