为什么我收到此错误类'String'没有实例方法'data'.接收者:"1037137041211097358pVz3E"? [英] Why im getting this error Class 'String' has no instance method 'data'. Receiver: "1037137041211097358pVz3E"?

查看:44
本文介绍了为什么我收到此错误类'String'没有实例方法'data'.接收者:"1037137041211097358pVz3E"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,但是我收到一条错误消息

Im dont know why but im getting an error that says

[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: type 'String' is not a subtype of type 'QuerySnapshot'
#0      _OpenalldocsState.getusers.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:wichtigdenyady/homesearchingall/openalldocs.dart:103
#1      State.setState
package:flutter/…/widgets/framework.dart:1267
#2      _OpenalldocsState.getusers.<anonymous closure>.<anonymous closure>
package:wichtigdenyady/homesearchingall/openalldocs.dart:102
#3      List.forEach (dart:core-patch/growable_array.dart:403:8)
#4      _OpenalldocsState.getusers.<anonymous closure>
package:wichtigdenyady/homesearchingall/openalldocs.dart:101
#5      _rootRunUnary (dart:async/zone.dart:1362:47)
#6      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
#7      _FutureListener.handleValue (dart:async/future_impl.dart:152:18)
#8      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:704:45)
#9      Future._propagateToListeners (dart:async/future_impl.dart:733:32)
#10     Future._completeWithValue (dart:async/future_impl.dart:539:5)
#11     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:254:13)
#12     Query.get (package:cloud_firestore/src/query.dart)
package:cloud_firestore/src/query.dart:1
<asynchronous suspension>


错误抛出在这里

 querySnapshot.docs.forEach((doc) {. 
        setState(() {                   
          listOfIds.add(doc["hashtag1"]);  
        });

所以我不知道为什么我实际上会收到此错误,但是我需要蜜蜂而不是字符串来录制文件,我认为目前是字符串.这是我的代码

So I dont know why im getting this error actually but I need that to bee a documetnsnapshot not string and I think that it is at the moment String. Heres my code

  List _allResults = [];
  List _resultsList = [];

getusers() async {
    var firestore = FirebaseFirestore.instance;
 List<DocumentSnapshot> listOfIds = [];

    await firestore
        .collection('videos')
        .get()
        .then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        setState(() {
          listOfIds.add(doc['hashtag1']);
        });
      });
    });
    if (!mounted) return;
    _allResults = listOfIds;
print(_allResults);
    searchResults();
    return "Complete";
  }

 @override
  Widget build(BuildContext context) {
    final user = Provider.of<Userforid>(context);
    if (nosuerfound == true) {
      return ListView.builder(
          itemCount: _resultsList.length,
          itemBuilder: (BuildContext context, int index) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // the AMOUNT is how many hashtags you want to show
                for (var i = 0; i < _resultsList.length; i += 1) ...[
                  // the SizedBox will only exist between the elements in the list
                  // as before
                  if (i != 0) SizedBox(height: 6),
                  // create a builder to allow declaring a variable
                  Builder(
                    builder: (context) {
                      // declare the hashtag variable
                      final  hashtag = 'hashtag${i + 1}';

                      return InkWell(
                        onTap: () {
                          // do something with the hashtag stored in the variable
                          // this will make it relative to the element in the list
                        },
                        child: Column(
                          children: <Widget>[
                            // why is there a Column inside another with only one child?
                            // I would recommend to remove it
                            Column(
                              children: [
                                HighlightedMatchesText(
                                  searchString: widget.searchinginput.text,
                                  // notice how I am using the hashtag variable here
                                  // instead of a constant? ('hashtag1'), by the way
                                  // the for loop will make the hashtag start at 0
                                  // you can change it by increment in the declaration
                                  // `final hashtag = 'hashtag${i+1}'`, if you want
                                  // the existing behavior
                                  content: _resultsList[index].data[hashtag],
                                ),
                              ],
                            ),
                            // what is this? if it is to add more space between the items
                            // in the list, I recommend removing it from here, and add it
                            // to the first `SizedBox` in the for loop
                            // in case you do that, the Column that this widget belong
                            // would also only now contain one widget, so, there is no
                            // need to have it
                            SizedBox(height: 3),
                          ],
                        ),
                  

我使用for循环获取所有数据,然后用户可以在此列表中进行搜索.这是我的firebase

Im using a for loop to get all data printed and then user can search inside this list. This is my firebase

所以应该解决的问题在这里

So the problem that should be solved is this here

 querySnapshot.docs.forEach((doc) {. 
        setState(() {                   
          listOfIds.add(doc["hashtag1"]);  
        });

应该不是获取String列表的方法,而应该是获取documetnsnapshots的方法.然后列表_allResults应该是这些文档快照的列表,类似这样

Instead of being a list of String it should me a method where I get documetnsnapshots. And then the list _allResults should be a list of these document snapshots something like this

 
    var firestore = FirebaseFirestore.instance;
    QuerySnapshot qn = await firestore.collection('videos').get();
    if (!mounted) return;

    setState(() {
      _allResults = qn.docs;
    });
    searchResults();
    return "Complete";
  }

实际上是hastag1字段;

But actually the hastag1 field;

推荐答案

getusers() async {
    var firestore = FirebaseFirestore.instance;
    List<String> listOfIds = [];

    await firestore
        .collection('videos')
        .get()
        .then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        
          listOfIds.add(doc["hashtag1"]);
      
      });
    });

尝试并提供反馈

这篇关于为什么我收到此错误类'String'没有实例方法'data'.接收者:"1037137041211097358pVz3E"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆