skip to Main Content

I am making a form for user to add new sellers by inputing the seller’s account name and type of account. But before the new seller is added to the firestore, it will check whether the seller has exist or not. Right now my code only checks if the entered name is very similar to the one in the database.

For example: In the database, there is SellerABC, so if user enter,

  • sellerABC = not same
  • seLLerAbc = not same
  • SellerABC = same

Since the seller’s account name could be in combination of uppercase and lowercase, I want to check similar letters of the two strings. If the name entered by user has the same alphabets as the one in the database it will return true.

For example:

  • SeLLerabC = same

How should I check the string for the same letters?

Here is my code:

    Future addSeller({
    required String seller_name,
    required String? typeOfAcc,
  }) async {
    
    final docSeller = FirebaseFirestore.instance.collection('sellers').doc();
    final querySnapshot = FirebaseFirestore.instance
        .collection('sellers')
        .where('seller_name', isEqualTo: seller_name)
        .where('account_type', isEqualTo: typeOfAcc);

querySnapshot.get().then((querySnapshot) async {
      if (querySnapshot.docs.isNotEmpty) {
        showDialog(
            context: context,
            barrierDismissible: false,
            builder: (context) => AlertDialog(
                  title: Text('Error'),
                  content: Text('This seller already exists!'),
                  actions: <Widget>[
                    TextButton(
                      child: Text(
                        'OK',
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      style: ButtonStyle(
                          backgroundColor: MaterialStateProperty.all(
                              Color.fromARGB(255, 43, 115, 255))),
                    ),
                  ],
                ));
      } else {
        final json = {
          'seller_name': seller_name,
          'account_type': typeOfAcc,
        };
        await docSeller.set(json);
        showDialog(
            context: context,
            barrierDismissible: false,
            builder: (context) => AlertDialog(
                  title: Text('Successful'),
                  content: Text('New seller has been successfully added!'),
                  actions: <Widget>[
                    TextButton(
                      child: Text(
                        'OK',
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => EvaluateCriteria(
                                    sellerName: seller_name,
                                    typeAcc: typeOfAcc)));
                      },
                      style: ButtonStyle(
                          backgroundColor: MaterialStateProperty.all(
                              Color.fromARGB(255, 43, 115, 255))),
                    ),
                  ],
                ));
      }
    });
  }
}

2

Answers


  1. Firestore does not offer case-insensitive queries. If you need to query for strings and ignore their case, you should store a "canonical" version of the string in Firestore, then make the client code use that for all queries.

    So, if you need the ‘original’ seller name, you could keep an extra field called ‘serachable_seller_name’ (that will hold the lowercase seller name) and query the documents based on that, and when displaying, you display the ‘seller_name’, which is the seller name with uppercase characters.

    Login or Signup to reply.
  2. Either you create separate field to keep everything in small case, or use full text search solutions that can be combined with firestore easily
    like
    Algolia, Elastic search etc.
    https://firebase.google.com/docs/firestore/solutions/search

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