Flutter 如何使用Flatter在firestore中检索特定文档
我正在尝试在我的应用程序中检索firestore文档,以便更新它。以下是当前代码:Flutter 如何使用Flatter在firestore中检索特定文档,flutter,google-cloud-firestore,flutter-showmodalbottomsheet,Flutter,Google Cloud Firestore,Flutter Showmodalbottomsheet,我正在尝试在我的应用程序中检索firestore文档,以便更新它。以下是当前代码: import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; void main() { runApp( MyApp(), ); } class MyApp extends StatefulWidget { @override _MyAppState cre
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
MyApp(),
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Baby Names',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() {
return _MyHomePageState();
}
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Baby Name Votes')),
body: _buildBody(context),
);
}
Widget _buildBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('baby').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildList(context, snapshot.data.documents);
},
);
}
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
return ListView(
padding: const EdgeInsets.only(top: 20.0),
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
);
}
Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
final record = Record.fromSnapshot(data);
final docID = record.reference.documentID;
return Padding(
key: ValueKey(record.name),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: ListTile(
title: Text(record.name),
trailing: Text(record.votes.toString()),
onTap: () {
print('Here is the record you have just clicked on: $docID, ${record.name}, ${record.votes}');
showModalBottomSheet(
context: context,
builder: (context) => EditVoteScreen(),
);
},
),
),
);
}
}
class EditVoteScreen extends StatefulWidget {
@override
_EditVoteScreenState createState() => _EditVoteScreenState();
}
class _EditVoteScreenState extends State<EditVoteScreen> {
String newBabyName = 'Gregg';
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xff757575),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0), topRight: Radius.circular(20.0)),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Edit A Baby Name'),
SizedBox(height: 20.0),
Text(
'Current Name: ${record.name}',
),
SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Change Baby Name To:',
),
SizedBox(
width: 20.0,
),
DropdownButton<String>(
value: newBabyName,
icon: Icon(Icons.arrow_drop_down_circle),
iconSize: 24,
elevation: 16,
underline: Container(
height: 1,
color: Color(0xFF150A42),
),
onChanged: (String newValue) {
setState(() {
newBabyName = newValue;
});
},
items: <String>['Gregg', 'Mikey', 'Joey', 'Dave']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
SizedBox(height: 20.0),
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0)),
color: Color(0xFF150A42),
textColor: Colors.white,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Text(
'You need to add a constructor to your EditVoteScreen widget and pass in the document's information so you can use it in your EditVoteScreen's build method:
class EditVoteScreen extends StatefulWidget {
final Record record;
const EditVoteScreen({Key key, this.record}) : super(key: key);
@override
_EditVoteScreenState createState() => _EditVoteScreenState();
}
import'包:cloud_firestore/cloud_firestore.dart';
进口“包装:颤振/材料.省道”;
void main(){
runApp(
MyApp(),
);
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“婴儿名字”,
主页:MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState(){
返回_MyHomePageState();
}
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“婴儿名字投票”),
正文:_buildBody(上下文),
);
}
Widget\u buildBody(BuildContext上下文){
返回流生成器(
流:Firestore.instance.collection('baby').snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)返回LinearProgressIndicator();
返回构建列表(上下文、快照、数据、文档);
},
);
}
小部件构建列表(构建上下文上下文,列表快照){
返回列表视图(
填充:仅限常量边集(顶部:20.0),
子项:snapshot.map((数据)=>\u buildListItem(上下文,数据)).toList(),
);
}
小部件_buildListItem(BuildContext上下文,DocumentSnapshot数据){
最终记录=记录。从快照(数据);
最终文档ID=record.reference.documentID;
返回填充(
key:ValueKey(记录名称),
填充:常量边集。对称(水平:16.0,垂直:8.0),
子:容器(
装饰:盒子装饰(
边框:边框。全部(颜色:颜色。灰色),
边界半径:边界半径。圆形(5.0),
),
孩子:ListTile(
标题:文本(记录名称),
尾随:文本(record.vows.toString()),
onTap:(){
print('这是您刚才单击的记录:$docID,${record.name},${record.voces}');
showModalBottomSheet(
上下文:上下文,
生成器:(上下文)=>EditVoteScreen(),
);
},
),
),
);
}
}
类EditVoteScreen扩展StatefulWidget{
@凌驾
_EditVoteScreenState createState()=>\u EditVoteScreenState();
}
类_EditVoteScreenState扩展状态{
字符串newBabyName='Gregg';
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:颜色(0xFF7575),
子:容器(
装饰:盒子装饰(
颜色:颜色,白色,
borderRadius:仅限borderRadius(
左上角:半径.圆形(20.0),右上角:半径.圆形(20.0)),
),
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(“编辑婴儿姓名”),
尺寸箱(高度:20.0),
正文(
'当前名称:${record.Name}',
),
尺寸箱(高度:20.0),
划船(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
'将婴儿姓名更改为:',
),
大小盒子(
宽度:20.0,
),
下拉按钮(
值:newabyname,
图标:图标(图标。箭头\下拉\圆圈),
iconSize:24,
海拔:16,
下划线:容器(
身高:1,,
颜色:颜色(0xFF150A42),
),
onChanged:(字符串newValue){
设置状态(){
newBabyName=newValue;
});
},
物品:['Gregg','Mikey','Joey','Dave']
.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
}).toList(),
),
],
),
尺寸箱(高度:20.0),
扁平按钮(
形状:圆形矩形边框(
边界半径:边界半径。圆形(15.0)),
颜色:颜色(0xFF150A42),
textColor:Colors.white,
孩子:填充(
填充:常量边集。对称(垂直:12.0),
子:文本(
“您需要向EditVoteScreen小部件添加构造函数,并传入文档信息,以便在EditVoteScreen的构建方法中使用它:
showModalBottomSheet(
context: context,
builder: (context) => EditVoteScreen(record: record,),
);
创建类时传入记录:
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Edit A Baby Name'),
SizedBox(height: 20.0),
Text(
'Current Name: ${widget.record.name}', // Here
),
然后通过引用widget变量在state类中引用它
child:Column(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(“编辑婴儿姓名”),
尺寸箱(高度:20.0),
正文(
'当前名称:${widget.record.Name}',//此处
),
在收集后键入.doc(baby/id)
。非常感谢您的完美工作!现在完全有意义了!@Jwildsmith您为我节省了很多时间,非常感谢您。