skip to Main Content

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
          },

4

Answers


  1. Chosen as BEST ANSWER

    this line solve the problem :

    String gg = await FirebaseFirestore.instance
          .collection('carsData')
          .where('uid', isEqualTo: loggedInUser.uid)
          .where('CarName', isEqualTo: nameCar)
          .limit(1)
          .get()
          .then((value) => value.docs.first.id);
    

    but when you have 2 items have the same CarName you must add another where() to get specific id.


  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4.     FirebaseFirestore.instance
                .collection('carsData')
                .where('uid', isEqualTo: 'selected_car_uid')
                .get()
                .then((value) {
              value.docs.forEach((element) {
                print(element.id); // you will get your firestore id and then delete via this id.
    
        FirebaseFirestore.instance
                .collection("carsData")
                
                .doc(element.id)
                 
                .delete()
                .then((value_2) {
              print('========> successfully deleted');
            });
    
              });
            });
    

    HAPPY CODING 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search