我已经在Flutter中创建了一个应用,用于我公司的库存。应用程序中的一个小部件是一个DropdownButton,其中填充了Firebase Cloud Firestore中的元素。所有菜单项均已正确加载,但是当我选择一个项目时,会出现红色屏幕。
我对Flutter还是很陌生,所以我大多是从教程中复制代码的,所以奇怪的是它不起作用。
class _ReviewChangesScreenState extends State<ReviewChangesScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
DocumentSnapshot job;
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text('Review Changes'),
),
body: Column(children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Form(
key: _formKey,
child: StreamBuilder(
stream: Firestore.instance.collection('jobs').snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new DropdownButton(
value: job,
onChanged: (val) {
setState(() {
job = val;
});
},
items: snapshot.data.documents
.map<DropdownMenuItem<DocumentSnapshot>>(
(DocumentSnapshot document) {
return new DropdownMenuItem<DocumentSnapshot>(
child: Text(document.data['jobCode']),
value: document,
);
}).toList(),
);
},
)),
),
]));
}
}
这是我尝试在菜单中选择一个项目时收到的错误。
I/flutter ( 7320): The following assertion was thrown building StreamBuilder<QuerySnapshot>(dirty, state:
I/flutter ( 7320): _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#22b54):
I/flutter ( 7320): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 609 pos 15: 'items == null ||
I/flutter ( 7320): items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value ==
I/flutter ( 7320): value).length == 1': is not true.
Flutter Doctor输出:
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, v1.5.4-hotfix.2, on Microsoft Windows [Version 10.0.17763.529],
locale en-CA)
[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[√] Android Studio (version 3.3)
[√] Connected device (1 available)
我对问题的实质感到困惑。
答案 0 :(得分:0)
该错误告诉您Dropdown构造函数的items
或value
参数不满足Dropdown的要求。看一下代码:
assert(items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1)
人工翻译:
null
null
value
的元素您的输入数据违反了这些条件之一。