Emir Bolat
Emir Bolat

Reputation: 1049

How to Shrink Characters of Incoming Data from Flutter Firestore?

I wrote a code like this:

StreamBuilder(
    stream: _firestore.collection("Products").where("Name", isGreaterThanOrEqualTo: SearchText.toLowerCase()).snapshots(),
    builder: (BuildContext context, snapshot) {
      if (snapshot.data == null) {
        return const Text("No data");
      }
      return ListView.builder(
        itemCount: snapshot.data.docs.length,
        itemBuilder: (context, index) {
          return Card(
            child: ListTile(
              title: Text(snapshot.data.docs[index].data()["Name"]),
            ),
          );
        },
      );
    }
),

I want to shrink the data from Firestore with .toLowerCase() or otherwise. In order to make a search system, I need to shrink the incoming data. How can I do that?

Thanks for help.

Upvotes: 0

Views: 51

Answers (1)

Vipin Kumar Kashyap
Vipin Kumar Kashyap

Reputation: 253

I don't understand what you mean by shrink. You mentioned toLowerCase() so this is what I think the problem is

You have a stream of product names from firestore and you want to be able to make them searchable. The user search query text might be lowercase so you want to run your search on the products from firestore(lowercased)

One way to do this is to modify the stream of products that you are getting from your firestore . You can run this on dartpad.dev/

Here is a simple example with a fake list of products. I have illustrated how to use something called a streamTransformer

 // A mock list of products
final List<String> productList = [
  "Airpods",
  "Wallet",
  "Glasses",
  "Gatorade",
  "Medicine"
];

// A stream that exposes the product list
Stream<String> productStream() async* {
  for(var product in productList){
    yield product;
  }
}



void main() {
  // Use a stream transformer to transform or modify the stream
 StreamTransformer<String, dynamic> lowerCaser = StreamTransformer.fromHandlers(handleData: (data,sink)=> sink.add(data.toString().toLowerCase()));

  
  // Transform the stream with the .transform function
  productStream().transform(lowerCaser).listen(
  (product)=>print(product)
  );
  
}

Upvotes: 1

Related Questions