Flutter 在练习streamBuilder时,出现了一个错误
我试过火炉店 我将代码包装在一个streambuilder中,以便为提升的按钮提供一个文档 但是我弄错了 类型Flutter 在练习streamBuilder时,出现了一个错误,flutter,dart,Flutter,Dart,我试过火炉店 我将代码包装在一个streambuilder中,以便为提升的按钮提供一个文档 但是我弄错了 类型'MappedListIterable'不是类型“List”的子类型 我希望updatepage的textformfield不是空的,而是加载存储的数据 我怎样才能解决这个问题 我不知道该怎么办 这是CRUD的更新页面 import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; impo
'MappedListIterable'
不是类型“List”的子类型
我希望updatepage的textformfield不是空的,而是加载存储的数据
我怎样才能解决这个问题
我不知道该怎么办
这是CRUD的更新页面
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class Update extends StatefulWidget {
@override
_UpdateState createState() => _UpdateState();
}
class _UpdateState extends State<Update> {
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
TextEditingController _titleController = TextEditingController();
TextEditingController _descController = TextEditingController();
final db = Firestore.instance;
DocumentSnapshot _currentDocument;
_updateData() async {
await db
.collection('flutter_data2')
.document(_currentDocument.documentID)
.updateData({'title': _titleController.text});
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return MaterialApp(
home: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text('update'),
),
body: _buildUpdate(context)));
}
// I wrapped the code in a streambuilder to give the RaisedButton a doc
Widget _buildUpdate(BuildContext context) {
final Size size = MediaQuery.of(context).size;
//It consists of two textfields and raisedbuttons.
return StreamBuilder<QuerySnapshot>(
stream: db.collection('flutter_data2').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: snapshot.data.documents.map<Widget>((doc) {
return Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(20.0),
child: Card(
elevation: 2.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0)),
child: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.only(left: 12, right: 12),
child: Column(
children: <Widget>[
TextFormField(
controller: _titleController,
decoration: InputDecoration(labelText: 'title'),
validator: (String value) {
if (value.isEmpty) {
return 'title empty';
} else {
return null;
}
},
),
TextFormField(
controller: _titleController,
decoration: InputDecoration(labelText: 'desc'),
validator: (String value) {
if (value.isEmpty) {
return 'desc empty';
} else {
return null;
}
},
),
],
),
),
),
),
),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0)),
child: Text('update'),
color: Colors.blue,
onPressed: () async {
if (_formKey.currentState.validate()) {
_updateData();
_titleController.text = doc.data[
'title']; //To write this code, I covered everything with a streambuilder.
}
},
),
],
);
}),
);
} else {
return SizedBox();
}
},
);
}
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
导入“包:cloud_firestore/cloud_firestore.dart”;
类更新扩展了StatefulWidget{
@凌驾
_UpdateState createState()=>\u UpdateState();
}
类_UpdateState扩展状态{
GlobalKey _formKey=GlobalKey();
TextEditingController _titleController=TextEditingController();
TextEditingController _descController=TextEditingController();
final db=Firestore.instance;
文档快照_currentDocument;
_updateData()异步{
等待数据库
.collection('flatter_data2')
.document(_currentDocument.documentID)
.updateData({'title':\ titleController.text});
}
@凌驾
小部件构建(构建上下文){
最终大小=MediaQuery.of(context).Size;
返回材料PP(
家:脚手架(
resizeToAvoidBottomInset:false,
appBar:appBar(
标题:文本(“更新”),
),
正文:_buildUpdate(context));
}
//我将代码包装在streambuilder中,为RaisedButton提供一个文档
小部件构建更新(构建上下文){
最终大小=MediaQuery.of(context).Size;
//它由两个文本字段和升起的按钮组成。
返回流生成器(
流:db.collection('flatter_data2').snapshots(),
生成器:(上下文,快照){
if(snapshot.hasData){
返回列(
子项:快照.数据.文档.映射((doc){
返回列(
儿童:[
填充物(
填充:所有边缘设置(20.0),
孩子:卡片(
标高:2.0,
形状:圆形矩形边框(
边界半径:边界半径。圆形(16.0)),
孩子:表格(
键:_formKey,
孩子:填充(
填充:仅限边设置(左:12,右:12),
子:列(
儿童:[
TextFormField(
控制器:\标题控制器,
装饰:输入装饰(标签文本:“标题”),
验证器:(字符串值){
if(value.isEmpty){
返回“title empty”;
}否则{
返回null;
}
},
),
TextFormField(
控制器:\标题控制器,
装饰:输入装饰(标签文本:“desc”),
验证器:(字符串值){
if(value.isEmpty){
返回'desc empty';
}否则{
返回null;
}
},
),
],
),
),
),
),
),
升起的按钮(
形状:圆形矩形边框(
边界半径:边界半径。圆形(15.0)),
子项:文本('update'),
颜色:颜色,蓝色,
onPressed:()异步{
if(_formKey.currentState.validate()){
_更新数据();
_titleController.text=doc.data[
'title'];//为了编写这段代码,我用streambuilder涵盖了所有内容。
}
},
),
],
);
}),
);
}否则{
返回SizedBox();
}
},
);
}
}
映射文档后,必须添加toList()
另一种处理方法是使用:
children: [
for (final doc in snapshot.data.documents)
Column(
...
)
]