正文可能会正常完成,从而导致返回"null",但返回类型"Widget"可能是不可为空的类型。颤振码



我一直在flutter中显示一个下拉按钮,该按钮从云Firestore集合中提取数据,但最终出现了错误。如有任何反馈,我们将不胜感激。以下是我正在处理的代码片段:

child: Column(
children: <Widget>[
const SizedBox(
height: 10,
),
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('cars').snapshots(),
builder: (context, snapshot)
{
if (!snapshot.hasData) {
const Text("Loading");
} else {
List<DropdownMenuItem> carItems = [];
for (int i = 0; i < snapshot.data!.docs.length; i++) {
DocumentSnapshot snap = snapshot.data!.docs[i];
carItems.add(
DropdownMenuItem(
value: "${snap.id}",
child: Text(
snap.id,
style: const TextStyle(color: Colors.deepOrange),
),
),
);
}
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Icon(
Icons.car_repair,
size: 25.0,
),
const SizedBox(
width: 50.0,
),
DropdownButton<dynamic>(
items: carItems,
onChanged: (carValue) {
final snackBar = SnackBar(
content: Text(
'Selected Car is $carValue',
style: const TextStyle(color: Colors.grey),
),
);
Scaffold.of(context).showSnackBar(snackBar);
setState(() {
selectedCar = carValue;
});
},
value: selectedCar,
isExpanded: false,
hint: const Text(
"Choose Car Make",
style: TextStyle(color: Colors.deepOrange),
),
),
],
);
}
}

快照中没有数据条件,返回缺少

if (!snapshot.hasData) {
return Text("Loading");
}

相关内容

最新更新