【发布时间】:2019-03-20 06:47:52
【问题描述】:
我在下拉菜单和 Firestore 方面遇到问题,如何将从 firestore 检索到的文档列表绑定到 DropdownButton?目前我遇到了这个错误:
The argument type '(Map<dynamic, dynamic>) → DropdownMenuItem<String>' can't be assigned to the parameter type '(DocumentSnapshot) → dynamic'.
我的小部件 .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(),
);},
);
}
}
谢谢!
【问题讨论】:
-
删除类型提及 Map - snapshot.data.documents.map((Map map) to snapshot.data.documents.map((map) { ....
标签: firebase drop-down-menu dart flutter google-cloud-firestore