Firebase 如何在Flatter中将Firestore文档列表绑定到下拉菜单?
我在下拉菜单和firestore方面遇到问题,如何将从firestore检索到的文档列表绑定到下拉按钮?目前我有以下错误:Firebase 如何在Flatter中将Firestore文档列表绑定到下拉菜单?,firebase,drop-down-menu,dart,flutter,google-cloud-firestore,Firebase,Drop Down Menu,Dart,Flutter,Google Cloud Firestore,我在下拉菜单和firestore方面遇到问题,如何将从firestore检索到的文档列表绑定到下拉按钮?目前我有以下错误: The argument type '(Map<dynamic, dynamic>) → DropdownMenuItem<String>' can't be assigned to the parameter type '(DocumentSnapshot) → dynamic'. 参数类型“(映射)→ DropdownMenuItem“无法分配
The argument type '(Map<dynamic, dynamic>) → DropdownMenuItem<String>' can't be assigned to the parameter type '(DocumentSnapshot) → dynamic'.
参数类型“(映射)→ DropdownMenuItem“无法分配给参数类型”(DocumentSnapshot)→ “动态”。
来自my widget.dart的代码:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class MessageList extends StatelessWidget {
MessageList({this.firestore});
final Firestore firestore;
var _mySelection;
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: firestore.collection('preciso-modelos').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
return new DropdownButton<String>(
isDense: true,
hint: new Text("Select"),
value: _mySelection,
onChanged: (String newValue) {
print (_mySelection);
},
items: snapshot.data.documents.map((Map map) {
return new DropdownMenuItem<String>(
value: map["id"].toString(),
child: new Text(
map["name"],
),
);
}).toList(),
);},
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:cloud_firestore/cloud_firestore.dart”;
类MessageList扩展了无状态小部件{
MessageList({this.firestore});
最终消防仓库消防仓库;
var_mySelection;
@凌驾
小部件构建(构建上下文){
返回流生成器(
流:firestore.collection('preciso-modelos').snapshots(),
生成器:(BuildContext上下文,异步快照){
如果(!snapshot.hasData)返回常量文本('Loading…');
返回新的下拉按钮(
是的,
提示:新文本(“选择”),
值:_mySelection,
onChanged:(字符串newValue){
印刷品(选举);;
},
项目:快照.数据.文档.映射((映射){
返回新的DropdownMenuItem(
值:映射[“id”]。toString(),
儿童:新文本(
映射[“名称”],
),
);
}).toList(),
);},
);
}
}
谢谢 新建StreamBuilder(
new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('categories').snapshots(),
builder: (context, snapshot){
if (!snapshot.hasData) return const Center(
child: const CupertinoActivityIndicator(),
);
var length = snapshot.data.documents.length;
DocumentSnapshot ds = snapshot.data.documents[length - 1];
_queryCat = snapshot.data.documents;
return new Container(
padding: EdgeInsets.only(bottom: 16.0),
width: screenSize.width*0.9,
child: new Row(
children: <Widget>[
new Expanded(
flex: 2,
child: new Container(
padding: EdgeInsets.fromLTRB(12.0,10.0,10.0,10.0),
child: new Text("Category",style: textStyleBlueBold,),
)
),
new Expanded(
flex: 4,
child:new InputDecorator(
decoration: const InputDecoration(
//labelText: 'Activity',
hintText: 'Choose an category',
hintStyle: TextStyle(
color: primaryColor,
fontSize: 16.0,
fontFamily: "OpenSans",
fontWeight: FontWeight.normal,
),
),
isEmpty: _category == null,
child: new DropdownButton(
value: _category,
isDense: true,
onChanged: (String newValue) {
setState(() {
_category = newValue;
dropDown = false;
print(_category);
});
},
items: snapshot.data.documents.map((DocumentSnapshot document) {
return new DropdownMenuItem<String>(
value: document.data['title'],
child: new Container(
decoration: new BoxDecoration(
color: primaryColor,
borderRadius: new BorderRadius.circular(5.0)
),
height: 100.0,
padding: EdgeInsets.fromLTRB(10.0, 2.0, 10.0, 0.0),
//color: primaryColor,
child: new Text(document.data['title'],style: textStyle),
)
);
}).toList(),
),
),
),
],
),
);
}
);
流:Firestore.instance.collection('categories').snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)返回常量中心(
子项:常量CupertinoActivityIndicator(),
);
var length=snapshot.data.documents.length;
DocumentSnapshot ds=snapshot.data.documents[length-1];
_queryCat=snapshot.data.documents;
退回新货柜(
填充:仅限边缘设置(底部:16.0),
宽度:屏幕大小。宽度*0.9,
孩子:新的一排(
儿童:[
新扩展(
弹性:2,
子容器:新容器(
填充物:LTRB(12.0,10.0,10.0,10.0)的边缘设置,
子项:新文本(“类别”,样式:textStyleBlueBold,),
)
),
新扩展(
弹性:4,
子:新输入装饰器(
装饰:常量输入装饰(
//labelText:“活动”,
hintText:“选择一个类别”,
hintStyle:TextStyle(
颜色:原色,
字体大小:16.0,
fontFamily:“OpenSAN”,
fontWeight:fontWeight.normal,
),
),
isEmpty:_category==null,
孩子:新的下拉按钮(
值:_类别,
是的,
onChanged:(字符串newValue){
设置状态(){
_类别=新值;
下拉=假;
印刷品(_类);
});
},
项目:snapshot.data.documents.map((DocumentSnapshot文档){
返回新的DropdownMenuItem(
值:document.data['title'],
子容器:新容器(
装饰:新盒子装饰(
颜色:原色,
边界半径:新边界半径。圆形(5.0)
),
高度:100.0,
填充:来自LTRB(10.0,2.0,10.0,0.0)的边缘设置,
//颜色:原色,
子项:新文本(document.data['title',样式:textStyle),
)
);
}).toList(),
),
),
),
],
),
);
}
);
删除类型提及Map-snapshot.data.documents.Map((Map-Map)到snapshot.data.documents.Map((Map){….我认为最好添加注释以帮助询问者理解
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('shops').snapshots(), builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(
child: CupertinoActivityIndicator(),
);
return Container(
padding: EdgeInsets.only(bottom: 16.0),
child: Row(
children: <Widget>[
Expanded(
flex: 2,
child: Container(
padding: EdgeInsets.fromLTRB(12.0, 10.0, 10.0, 10.0),
child: Text(
"Shop",
),
)),
new Expanded(
flex: 4,
child: DropdownButton(
value: shopId,
isDense: true,
onChanged: (valueSelectedByUser) {
_onShopDropItemSelected(valueSelectedByUser);
},
hint: Text('Choose shop'),
items: snapshot.data.documents
.map((DocumentSnapshot document) {
return DropdownMenuItem<String>(
value: document.data['plant_name'] +
' ' +
document.data['shop_type'],
child: Text(document.data['plant_name'] +
' ' +
document.data['shop_type']),
);
}).toList(),
),
),
],
),
);
});
void _onShopDropItemSelected(String newValueSelected) {
setState(() {
this.shopId = newValueSelected;
});
}