如何从 Cloud Firestore 数据库中检索数据集合
How to retrieve data collection from cloud firestore database
我正在尝试检索我的集合中的所有字段,但我只能检索集合中的一个字段 personalinfo
而不是集合的全部内容。有没有办法检索 personalinfo
集合中的所有字段?
这是我将数据保存到云 firestore 数据库的代码
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'),
),
],
),
),
)
);
}
}
这是我在 Cloud firestore 数据库中检索数据的代码
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(),
);
},
),
);
}
}
如果您可以访问一个字段,则意味着您也可以访问其余字段。
如果 document['field1']
有效,那么您可以编写 document['field2']
来获取第二个字段等。
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(),
);
},
),
);
}
}
// using ListTile widget
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(data['item1']),
subtitle: Text(data['item2']),
);
},
);
我正在尝试检索我的集合中的所有字段,但我只能检索集合中的一个字段 personalinfo
而不是集合的全部内容。有没有办法检索 personalinfo
集合中的所有字段?
这是我将数据保存到云 firestore 数据库的代码
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'),
),
],
),
),
)
);
}
}
这是我在 Cloud firestore 数据库中检索数据的代码
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(),
);
},
),
);
}
}
如果您可以访问一个字段,则意味着您也可以访问其余字段。
如果 document['field1']
有效,那么您可以编写 document['field2']
来获取第二个字段等。
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(),
);
},
),
);
}
}
// using ListTile widget
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(data['item1']),
subtitle: Text(data['item2']),
);
},
);