[英]How to get the ID of the document in a subcollection?
我想使用“食物”子集合中的數據進行 CRUD,但我不明白如何獲取子集合的 ID
restaurantCollection
.doc(uid)
.collection('food')
.doc(docId)
.update(data);
我知道這是為了更新 food 子集合中的字段,但我如何實際獲得docId
?
final CollectionReference restaurantCollection =
FirebaseFirestore.instance.collection('restaurant');
Future addFoodData(String description, String name, double oriPrice,
double salePrice, int pax) async {
return await restaurantCollection.doc(uid).collection('food').doc().set({
'ruid': uid, //i stored the parent's ID in the child
'name': name,
'description': description,
'oriPrice': oriPrice,
'salePrice': salePrice,
'pax': pax,
});
}
List<Food> _foodListFromSS(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
return Food(
name: doc.data()['name'] ?? '',
description: doc.data()['description'] ?? '',
oriPrice: doc.data()['oriPrice'] ?? 0.0,
salePrice: doc.data()['salePrice'] ?? 0.0,
pax: doc.data()['pax'] ?? 0,
);
}).toList();
}
Stream<List<Food>> get food {
return restaurantCollection
.doc(uid)
.collection('food')
.snapshots()
.map(_foodListFromSS);
}
^database.dart 這是我創建數據的方式。
class Menu extends StatefulWidget {
@override
_MenuState createState() => _MenuState();
}
class _MenuState extends State<Menu> {
bool loading = false;
@override
Widget build(BuildContext context) {
final user = Provider.of<Users>(context);
return loading
? Loading()
: StreamProvider<List<Food>>.value(
initialData: [],
value: DatabaseService(uid: user.uid).food,
child: Scaffold(
appBar: ElrAppBar('Menu', false),
drawer: ElrDrawer(),
floatingActionButton: FloatingActionButton(
onPressed: () {
print('ADD MENU Pressed');
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MenuAdd(),
),
);
},
child: const Icon(Icons.add),
),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
width: double.infinity,
color: Colors.grey,
child: FoodList(),
),
),
);
}
}
^菜單.dart
class FoodList extends StatefulWidget {
@override
_FoodListState createState() => _FoodListState();
}
class _FoodListState extends State<FoodList> {
bool loading = false;
@override
Widget build(BuildContext context) {
final food = Provider.of<List<Food>>(context);
return loading
? Loading()
: ListView.builder(
itemCount: food.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () => Navigator.push(context,
MaterialPageRoute(builder: (context) => MenuUpdate())),
child: Card(
margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
child: FoodTile(food: food[index])),
);
},
);
}
}
^food_list.dart
class FoodTile extends StatelessWidget {
final Food food;
FoodTile({this.food});
@override
Widget build(BuildContext context) {
return Card(
child: Container(
margin: EdgeInsets.all(5),
child: Row(
children: [
Container(
color: colorsConst[900],
width: 100,
height: 100,
),
SizedBox(width: 10),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
food.name.inCaps,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
SizedBox(height: 4),
Container(
width: 260,
child: Text(
food.description.inCaps,
style: TextStyle(fontSize: 15, fontStyle: FontStyle.italic),
),
),
SizedBox(height: 4),
foodInfoCard('Original price: ',
'RM ${food.oriPrice.toStringAsFixed(2).toString()}'),
SizedBox(height: 4),
foodInfoCard('Selling price: ',
'RM ${food.salePrice.toStringAsFixed(2).toString()}'),
SizedBox(height: 4),
foodInfoCard('Available pax: ', food.pax.toString()),
],
),
],
),
),
);
}
^food_tile.dart
這是我從子集合中獲取食物清單的方式。 當我單擊 food_list.dart 中的卡片時,我不知道如何僅從子集合中返回一個特定數據。
我遇到了一個死胡同,我很迷茫。 我希望有一些願意幫助我的人。 如果需要,請詢問更多信息。 先感謝您!
您應該已將該id
存儲在您的應用程序中。 您已經擁有父類別的uid
。 而且food
是不變的。 在更新之前如何獲取原始數據? 如果沒有 ID,則無法更新文檔。 如果您要在food
子集合中創建新文檔,請使用.set()
。
當您執行此操作時:
Future addFoodData(String description, String name, double oriPrice,
double salePrice, int pax) async {
return await restaurantCollection.doc(uid).collection('food').doc().set({
'ruid': uid,
'id': generateRandowId,//Create an ID and store it here.
'name': name,
'description': description,
'oriPrice': oriPrice,
'salePrice': salePrice,
'pax': pax,
});
}
將來當您獲取這些文檔時,它會有一個“ID”字段,您可以使用它來更新它。 因為您在子集合中有文檔的父ruid
和food
以及現在的id
。 我還建議您從這些文檔中創建對象\\模型。 在處理['keys']
時,這將使使用它們更容易,並且更不容易出錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.