简体   繁体   中英

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.

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

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)
  );
  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM