【问题标题】:List is showing null when should show the data from firestore当应显示来自 firestore 的数据时,列表显示为空
【发布时间】:2020-08-23 16:59:00
【问题描述】:

我想在 Firestore 数据中搜索,但该列表未显示来自 Firestore 的数据。我想用 Firestore 数据返回我的列表,但它没有显示我,我想根据我的选择在列表数据中搜索。

请告诉我哪里错了?

class serachDeligat extends SearchDelegate<String> {
  Firestore _firestore = Firestore.instance;
  String ref = 'items';

  Future<List<DocumentSnapshot>> getSearch() async =>
      await _firestore.collection(ref).getDocuments().then((snaps) {
        return snaps.documents;
      });
  List<Map> search = <Map>[];
  Future getDocs() async {
    search = await (await getSearch()).map((item) => item.data).toList();
    return search;
  }

  @override
  void initState() {
    getDocs();
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    // TODO: implement buildActions
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          print('$search');
        },
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    // TODO: implement buildLeading
    return IconButton(
      icon: Icon(Icons.arrow_back),
      onPressed: () {
        close(context, null);
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return Container(
      child: Center(),
    );
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Container();
  }
}

【问题讨论】:

    标签: firebase flutter google-cloud-firestore full-text-search flutter-test


    【解决方案1】:

    我将分享我在一个有同样问题的项目中所做的事情。我尝试了很多方法,但所有方法都以 null 结尾。然后我在 StreamSubscription 的帮助下更改了我的函数。这将更改数据即使在从 firestore 检索之后,因为它与 firestore 保持连接。我在 initstate 中加载了数据。

      StreamSubscription<QuerySnapshot> dailyTaskSubscription;
      ProgressDialog progressDialog;
      List<ScheduleTask> mondayTaskList = List();
      @override
      void initState() {
        super.initState();
          mondayTaskList = List();
          dailyTaskSubscription?.cancel();
          dailyTaskSubscription = scheduleService
              .getDailyTaskList(studentId, 'monday')
              .listen((QuerySnapshot snapshot) {
            final List<ScheduleTask> tasks = snapshot.documents
                .map((documentSnapshot) =>
                    ScheduleTask.fromMap(documentSnapshot.data))
                .toList();
        });
      }
    

    【讨论】:

    • super.initState();在 SearchDelegate 类中不起作用
    【解决方案2】:

    这是我的问题的答案 我应该在我的 onPressed 中使用 await

    class serachDeligat extends SearchDelegate<String>{
      Firestore _firestore = Firestore.instance;
      String ref = 'items';
      Future<List<DocumentSnapshot>> getSearch() async =>
          await _firestore.collection(ref).getDocuments().then((snaps) {
            return snaps.documents;
          });
      List<Map> search = <Map>[];
      Future getDocs() async {
        search=await
            (await getSearch()).map((item) => item.data).toList();
        return search;}
      @override
      void initState() {
        getDocs();
        super.query;
      }
      List<Map> searchAi = <Map>[];
      @override
      List<Widget> buildActions(BuildContext context) {
        // TODO: implement buildActions
        return[IconButton(
          icon: Icon(Icons.clear),
          onPressed: ()async {
            searchAi=await getDocs();
    
            print(searchAi);
    
          },),];}
    
      @override
      Widget buildLeading(BuildContext context) {
        // TODO: implement buildLeading
        return IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: (){
            close(context, null);
          },
        );
      }
    
      @override
      Widget buildResults(BuildContext context) {
    
        return Container(
          child: Center(),);}
    
      @override
      Widget buildSuggestions(BuildContext context) {
        return Container();}}
    

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      相关资源
      最近更新 更多