Flutter 开关布尔值
我正在制作一个在Firestore中切换布尔值的测试应用程序。现在我检索该值,这是真的。但当我点击Firestore中的开关,该值变为false时,该怎么办Flutter 开关布尔值,flutter,dart,Flutter,Dart,我正在制作一个在Firestore中切换布尔值的测试应用程序。现在我检索该值,这是真的。但当我点击Firestore中的开关,该值变为false时,该怎么办 StreamBuilder<List<Product>>( stream: ProductService().products, builder: (context, snapshot) { if (snapshot.hasData) { return DataTable(
StreamBuilder<List<Product>>(
stream: ProductService().products,
builder: (context, snapshot) {
if (snapshot.hasData) {
return DataTable(
columns: [
DataColumn(
label: Text('isActive'),
),
],
rows: snapshot.data.map((products) {
return DataRow(cells: [
DataCell(
Switch(
value: products.isActive,
onChanged: (bool notActive) {
setState(() {
products.isActive = notActive
});
}),
),
]);
}).toList(),
);
}
return Container();
});
StreamBuilder(
流:ProductService()。产品,
生成器:(上下文,快照){
if(snapshot.hasData){
返回数据表(
栏目:[
数据列(
标签:文本('isActive'),
),
],
行:snapshot.data.map((产品){
返回数据行(单元格:[
数据单元(
开关(
价值:products.isActive,
一旦更改:(bool notActive){
设置状态(){
products.isActive=notActive
});
}),
),
]);
}).toList(),
);
}
返回容器();
});
在
FireStore
中,当创建产品价值时,“isActive”始终为真。您必须定义如何在您的服务中设置FireStore中的价值,即ProductService
参考我的代码,了解如何在FireStore中更新文档
final Firestore db=Firestore.instance;
DocumentReference docRef=db.collection('collection_NAME')。document('document_NAME');
Future toggleActive(布尔激活){
返回docRef.setData({
“isActive”:活动,
},merge:true);
}
希望能有帮助