【问题标题】:Search key inside array , how do access them in .where for searching数组内的搜索键,如何在 .where 中访问它们进行搜索
【发布时间】:2020-02-07 00:27:08
【问题描述】:

我想将我的搜索键存储在一个数组中,这样我就可以用一行代码搜索多个内容,但它不起作用。

我正在使用基本搜索,这正是https://github.com/rajayogan/flutterfirestore-instantsearch

我所拥有的基本上是多个具有数组的文档,一个数组由包含 item_name 等信息的地图组成……我要搜索的是 item_name。

我试着放了

import 'package:cloud_firestore/cloud_firestore.dart';


//This class should take the array search which is  that I wish to be my searchkeys


class SearchService {
  searchByName(String searchField) {
    return Firestore.instance
        .collection('Foodtruckinfo')
        .where('arraysearch',
        isEqualTo: searchField.substring(0, 1).toUpperCase())
        .getDocuments();
   }
 }



 import 'package:cloud_firestore/cloud_firestore.dart';
  import 'package:customer_register_login/Customer/searchmethod.dart';

  import 'package:flutter/material.dart';

     class MyHomePage extends StatefulWidget {
     @override
    _MyHomePageState createState() => new _MyHomePageState();
   }

    class _MyHomePageState extends State<MyHomePage> {
    var queryResultSet = [];
    var tempSearchStore = [];

    initiateSearch(value) {
     if (value.length == 0) {
       setState(() {
         queryResultSet = [];
         tempSearchStore = [];
        });
       }

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

     if (queryResultSet.length == 0 && value.length == 1) {
       SearchService().searchByName(value).then((QuerySnapshot docs) {
         for (int i = 0; i < docs.documents.length; ++i) {
           queryResultSet.add(docs.documents[i].data);
          }
       });
     } else {
        tempSearchStore = [];
        queryResultSet.forEach((element) {
         if (element['item_name'].startsWith(capitalizedValue)) {
           setState(() {
              tempSearchStore.add(element);
           });
         }
        });
     }
   }

    @override
   Widget build(BuildContext context) {
     return new Scaffold(
             appBar: new AppBar(
                title: Text('Firestore search'),
              ),
              body: ListView(children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: TextField(
                   onChanged: (val) {
                   initiateSearch(val);
               },
               decoration: InputDecoration(
                   prefixIcon: IconButton(
                     color: Colors.black,
                      icon: Icon(Icons.arrow_back),
                    iconSize: 20.0,
                    onPressed: () {
                        Navigator.of(context).pop();
                     },
                   ),
                    contentPadding: EdgeInsets.only(left: 25.0),
                    hintText: 'Search by name',
                   border: OutlineInputBorder(
                       borderRadius: BorderRadius.circular(4.0))),
              ),
            ),
                 SizedBox(height: 10.0),
                   GridView.count(
                  padding: EdgeInsets.only(left: 10.0, right: 10.0),
               crossAxisCount: 2,
                crossAxisSpacing: 4.0,
                mainAxisSpacing: 4.0,
                primary: false,
               shrinkWrap: true,
                children: tempSearchStore.map((element) {
                   return buildResultCard(element);
               }).toList())
          ]));
       }
   }

      Widget buildResultCard(data) {
        return Card(
             shape: RoundedRectangleBorder(borderRadius: 
    BorderRadius.circular(10.0)),
     elevation: 2.0,
      child: Container(
          child: Center(
              child: Text(
         data['item_name'],
         textAlign: TextAlign.center,
        style: TextStyle(
          color: Colors.black,
          fontSize: 20.0,
        ),
       )))); 
    }

[1]:https://ibb.co/47p2dBD

[2]: https://ibb.co/kQkWKsq

[3]: https://ibb.co/MVnBtCd

[4] https://ibb.co/yk04RJ4

【问题讨论】:

    标签: firebase flutter dart google-cloud-platform google-cloud-firestore


    【解决方案1】:

    我在 Google 的文档中进行了搜索,但没有找到关于 Dart 和 Firestore Collections 的任何内容。但是您可以尝试一种解决方法,from this third 聚会,您可以找到一种将 Node.js 与 Dart 结合使用的方法。此外,您可以关注 Google 官方的documentation 使用 Node.js 获取您的数据。

    【讨论】:

      猜你喜欢
      • 2020-02-01
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      相关资源
      最近更新 更多