我需要从 Firestore 中的文档填充 DropdownButton。我可以检索数据。当我查看 snapshot.data 时,我看到了 2 条记录,这是我希望看到的。在下面的代码中,只要我注释掉您所看到的代码片段,一切正常。
Container(
child: StreamBuilder(
//stream: _firestoreService.getAgency(),
stream: _db.collection('agency').snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Center(
child: CircularProgressIndicator(),
);
} else {
//var length = snapshot.data.docs.length;
//print('length: ' + length);
return new DropdownButton<String>(
hint: new Text("Select Agency"),
value: _currentAgency,
/* <<<< The code below is where I am having problems
//onChanged: changedDropDownState,
**items: snapshot.data.docs.map((Map map) {
return new DropdownMenuItem<String>(
value: map["name"].toString(),
child: new Text(
map["name"],
),
);
}).toList(),**
*/
);
}
;
}),
),
当我取消注释代码并运行应用程序时,我收到此错误:
======== Exception caught by widgets library =======================================================
The following _TypeError was thrown building StreamBuilder<QuerySnapshot>(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#d9273):
type '(Map<dynamic, dynamic>) => DropdownMenuItem<String>' is not a subtype of type '(QueryDocumentSnapshot) => dynamic' of 'f'
我想要完成的是使用文档 ID 填充 value: 属性,但我在 snapshot.data 中看不到它。我想做的另一件事是使用来自 snapshot.data 的一些值填充 child: 属性。
我该怎么做呢?