簡體   English   中英

在撲撲中使用Cloud Firestore執行搜索時出錯

[英]error in perform searching using cloud firestore in flutter

我在獲取搜索結果時遇到問題。 我在Cloud firestore有一些數據,我想對該數據執行搜索。 這是數據的屏幕截圖,但是當我搜索時,我無法獲取任何數據。

這是我要執行搜索的數據

這是代碼...

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:reminder/searchservice.dart';

class MySearchPage extends StatefulWidget {
  @override
  _MySearchPageState createState() => new _MySearchPageState();
}

class _MySearchPageState extends State<MySearchPage> {
  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['email'].startsWith(capitalizedValue)) {
          setState(() {
            tempSearchStore.add(element);
          });
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: Text('Search Here'),
        ),
        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 Email',
                  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['email'],
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 20.0,
                ),
              )
          )
      )
  );
}

這是服務代碼...

import 'package:cloud_firestore/cloud_firestore.dart';

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

我猜你的錯誤是在.where子句中。

這是一個非常有線的情況

.where('title', isEqualTo: searchField.substring(0, 1).toUpperCase())

並且僅當標題的首字母與您搜索的第一個字母相對應時,它才匹配。 所以我敢打賭,您的docs.documents.lenght為0。

仔細閱讀Cluod Firestore 查詢部分。

  1. 您可以使用這樣的簡單查詢:
 .where('title', isGreaterThan: searchField) 
  1. 或者,如果您想要更復雜的索引搜索,則可以嘗試使用復合索引

索引概述

  1. 但是,如果您想對搜索字符串(例如,包含等)進行更多推斷,則必須先進行查詢,然后在此處進行過濾:
 for (int i = 0; i < docs.documents.length; ++i) { //TODO: do more sofisticated filtering here } 
  1. 或者,如果可以的話,您可以創建一個額外的task field並使用您自己預先構建的“搜索字符串”填充此值,並以基本Cloud Firestore查詢應返回所需內容的方式進行填充。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM