I am trying to retrieve all the fields in my collection, but i am only able to retrieve one field in the collection personalinfo
instead of the entire content of the collection. Is there a way to retrieve all the fields in the collection of personalinfo
?
this are my code to save data to cloud firestore database
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grocery_shopping/pages/order_successful/order_successful.dart';
class PersonalInformation extends StatelessWidget {
PersonalInformation ({Key? key}) : super(key: key);
TextEditingController fullname = new TextEditingController();
TextEditingController address = new TextEditingController();
TextEditingController city = new TextEditingController();
TextEditingController zipcode = new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Checkout")),
body: Container(
padding: EdgeInsets.all(40.0),
child: Center(
child: Column(
children: [
TextFormField(
controller: fullname,
decoration: InputDecoration(
hintText: "Full Name"
),
),
SizedBox(
height: 10.0,
),
TextFormField(
controller: address,
decoration: InputDecoration(
hintText: "Address"
),
),
SizedBox(
height: 10.0,
),
TextFormField(
controller: city,
decoration: InputDecoration(
hintText: "City"
),
),
SizedBox(
height: 10.0,
),
TextFormField(
controller: zipcode,
decoration: InputDecoration(
hintText: "Zipcode"
),
),
SizedBox(
height: 10.0,
),
TextButton(
onPressed: (){
Map <String,dynamic> data= {"field1": fullname.text,"field2": address.text,"field3": city.text, "field4": zipcode.text};
FirebaseFirestore.instance.collection("personalinfo").add(data);
} ,
child: Text("Submit"),
),
TextButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (BuildContext context)=> OrderSuccessful()));
},
child: Text("Continue"),
),
ElevatedButton(
onPressed: () => Get.to(() => OrderSuccessful()),
child: Text('Continue'),
),
],
),
),
)
);
}
}
this are my code for the data retrieve in cloud firestore database
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class OrderSuccessful extends StatefulWidget {
@override
_OrderSuccessfulState createState() => _OrderSuccessfulState();
}
class _OrderSuccessfulState extends State<OrderSuccessful>{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Order Successful")),
resizeToAvoidBottomInset: true,
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('personalinfo').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if (!snapshot.hasData){
return Text("no value");
}
return ListView(
children: snapshot.data!.docs.map((document){
return Text(document['field1']);
}).toList(),
);
},
),
);
}
}
CodePudding user response:
If you are able to access one field, then it means that you can access the remaining ones too.
If document['field1']
works, then you can write document['field2']
to get the second field & so on.
class _OrderSuccessfulState extends State<OrderSuccessful>{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Order Successful")),
resizeToAvoidBottomInset: true,
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('personalinfo').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if (!snapshot.hasData){
return Text("no value");
}
return ListView(
children: snapshot.data!.docs.map((document){
// You can use a different widget like ListTile here if you want
return Text(document['field1'] " " document['field2'] " " document['field3'] " " document['field4']);
}).toList(),
);
},
),
);
}
}