0
var capitalValue = value.substring(0,1).toUpperCase() + value.substring(1);

    if(queryResult.length == 0 && value.length == 1) {
       SearchService().searchProduct(value).then((QuerySnapshot docs){
         for(int i = 0; i < docs.documents//here error .length; i++) {
           queryResult.add(docs.documents//here error [i].data);
         }
       });
    }
    else{
      tempSearch = [];
      queryResult.forEach((element) {
        if(element['category'].startsWith(capitalValue)) {
          setState(() {
            tempSearch.add(element); 
          });
        }
      });
    }
  }

The getter 'documents' isn't defined for the type 'QuerySnapshot<Object?>'.Currently, I'm coding to make a search in the application

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

I think you are following some old documentation or tutorial. As far as I remember, object of QuerySnapshot has been changed few months ago.

They have simplified the usage by changing documents to docs and document to doc.

So your code should be like this

SearchService().searchProduct(value).then((QuerySnapshot docs){
   for(int i = 0; i < docs.docs.length; i++) {
      queryResult.add(docs.docs[i].data);
   }
});

You can review the changelog here https://gist.github.com/Salakar/a36bd0902d7d0c705696da9318cf4e71

Updated - 03/01/2023
After testing few different codes with firebase, I was able to get some solution. But I tried that in javascript based code so I am making few changes which should work on flutter as well.

So just try these with your code & see if it works for you. To get the text based search you can use something like

users
    .where("name", isGreaterThanOrEqualTo: "Aa");

This will start searching for the names starting 'Aa' but will not stop on this and continue search after that.

To stop the searching after matching these following text try implementing something like this.

users
    .where("name", isGreaterThanOrEqualTo: "Aa")
    .where("name", isLessThanOrEqualTo: "Aa"+ '\uf8ff');

This will stop looking for record as soon as the first two digits changes. Similar code for js is working for me on svelte + firebase setup.

I still don't know how to search in between the text. But there are some third party tools you can use which are mentioned on there documents here https://firebase.google.com/docs/firestore/solutions/search.

I looked into some reference stack-overflow answers & some articles but unfortunately I don't have those tabs opens right now so can't share all the sources. One of those sources is this Google Firestore: Query on substring of a property value (text search)

Aakash Kumar
  • 1,069
  • 7
  • 11
  • how can i chat you here? because i wanna ask more question regarding flutter –  Dec 30 '22 at 10:39
  • Sorry, not sure about the chat functionality here. I tried something but I think chat system is currently down. You can ask it here in comment. – Aakash Kumar Dec 30 '22 at 10:46
  • how to code search function in flutter but retrieve the data from firebase. I have no idea bout that cause i still newbie in flutter like really basic. Can you help me? –  Dec 30 '22 at 10:49
  • Firebase does not support the text search as other sql db. You might have to use some third party tool for that or create some firebase function. But do keep in mind that it will increase the document count which can affect your pricing model on firebase. – Aakash Kumar Dec 30 '22 at 11:44
  • but can i make just a simple search one where when i search the name of the product and it can appear what product i want? is it still need to use third party tool/services? –  Dec 30 '22 at 11:52
  • For that you need to insert the exact name of the product. Otherwise if you try greaterThan, in that case it can show all the items which match the specific keywords and more alhabetically. – Aakash Kumar Dec 30 '22 at 13:03
  • can you help me how to code that? –  Dec 31 '22 at 09:08
  • class FetchUserList { var data = []; List results = []; String urlList = 'https://jsonplaceholder.typicode.com/market/'; Future> getuserList({String? query}) async { var url = Uri.parse(urlList); try { var response = await http.get(url); if (response.statusCode == 200) { –  Dec 31 '22 at 09:10
  • can someone help me how to replace the url jason to my own firestore data to retrieve datat from search function? –  Dec 31 '22 at 09:11
  • The code you shared above is not using firebase. Are you trying to implement search without firebase? – Aakash Kumar Jan 02 '23 at 11:43
  • No, i'm trying to implement search with firebase. That's above was just the example of API json code where i want to change it to firebase –  Jan 02 '23 at 13:59
  • Finally after testing some search functionality myself, I was able to get some useful code for you. Check my updated answer. – Aakash Kumar Jan 03 '23 at 12:27
  • Thanks so much ! I'm gonna review all info. Unfortunately, I was blocked by sending any post or question here. I'm so sad cause i should finish my final year project for my degree :( . Glad you can help me :) –  Jan 03 '23 at 15:32