Home > Net >  Im trying to write code to get (doc id) from Firebase
Im trying to write code to get (doc id) from Firebase

Time:08-31

Look at my database structure:

enter image description here

and here is my code that I want to use ID in :

 Widget build(BuildContext context) {
    return SafeArea(
      child: InkWell(
        borderRadius: BorderRadius.circular(30),
        child: Dismissible(
          key: UniqueKey(),
          direction: DismissDirection.startToEnd,
          background: Container(
            color: Colors.red,
            child: Row(
              children: [
                Icon(Icons.delete),
                Text(
                  'Move to trash',
                  style: TextStyle(
                    color: Colors.white,
                    fontFamily: 'Righteous',
                  ),
                )
              ],
            ),
          ),
          confirmDismiss: (DismissDirection direction) async {
            return await showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: Text("Delete Confirmation"),
                    content: Text("Are you sure you want to delete this item?"),
                    actions: <Widget>[
                      TextButton(
                          onPressed: () => Navigator.of(context).pop(true),
                          child: const Text("Delete")),
                      TextButton(
                        onPressed: () => Navigator.of(context).pop(false),
                        child: const Text("Cancel"),
                      ),
                    ],
                  );
                });
          },
          onDismissed: (DismissDirection direction) async {
            if (direction == DismissDirection.startToEnd) {
              print('item deleted');
            }
            await deleteCar(
                'wam4jSgeIpWHIBLVXvmv'); //I want to get doc ID to delete it
          },

CodePudding user response:

There is some way:

FirebaseFirestore.instance
    .collection('$YOUR_COLLECTION')
.where('uid',  isEqualTo: "$UID_OF_THAT_ITEM").limit(1).get().then((value) => value.docs.first.id);

As you get it value.docs.first.id is what you need.

CodePudding user response:

Not sure if I understand what you triying to achieve. But the way I see it, you can duplicate that id as an atribute of the element when you create it for example.

"aasdasd" :{
"id": "aasdasd",
"carName": "car", 
}

or when you map cars, use the key you got as an atribute of your Car Model. This is an example for products.

  static Future loadProducts() async {
    final url = Uri.https(_baseUrl, 'products.json');
    final res = await http.get(url);
    final Map<String, dynamic> productsMap = json.decode(res.body);
    final List<Product> products = productsMap.keys
        .map((key) => Product(
              id: key,
              name: productsMap[key]['name'],
              description: productsMap[key]['description'],
              price: productsMap[key]['price'],
              imagen: productsMap[key]['imagen'],
              isAvailable: productsMap[key]['isAvailable'],
            ))
        .toList();

    return products;
  }

'key' is the value you want.

  • Related