【问题标题】:Class <QuerySnapshot> has no instance getter 'documents'.Reciever : Instance of 'QuerySnapshot'<QuerySnapshot> 类没有实例 getter 'documents'。Reciever : 'QuerySnapshot' 的实例
【发布时间】:2020-05-02 17:20:12
【问题描述】:

我有一个待办事项应用程序,它在屏幕中列出了所有待办事项 这是我的列表视图

class assignedTask extends StatefulWidget {
  static const String id = 'assignedTask';
  @override
  _assignedTaskState createState() => _assignedTaskState();
}

class _assignedTaskState extends State<assignedTask> {
  String Title;
  String Summary;

  var tasks;

  crudMedthods crudObj = new crudMedthods();

  var documentID;
  var documents;



  Future<bool> addDialog(BuildContext context) async {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Add Data', style: TextStyle(fontSize: 15.0)),
            content: Container(
              height: 125.0,
              width: 150.0,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  TextField(
                    decoration: InputDecoration(hintText: 'Enter Title'),
                    onChanged: (value) {
                      this.Title = value;
                    },
                  ),
                  SizedBox(height: 5.0),
                  TextField(
                    decoration: InputDecoration(hintText: 'Enter Summary'),
                    onChanged: (value) {
                      this.Summary = value;
                    },
                  ),
                ],
              ),
            ),
            actions: <Widget>[
              FlatButton(
                child: Text('Add'),
                textColor: Colors.blue,
                onPressed: () {
                  Navigator.of(context).pop();
                  crudObj.addData({
                    'Title': this.Title,
                    'Summary': this.Summary
                  }).then((result) {
                    dialogTrigger(context);
                  }).catchError((e) {
                    print(e);
                  });
                },
              )
            ],
          );
        });
  }

  Future<bool> updateDialog(BuildContext context, selectedDoc) async {

    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Update Data', style: TextStyle(fontSize: 15.0)),
            content: Container(
              height: 125.0,
              width: 150.0,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  TextField(
                    decoration: InputDecoration(hintText: 'Title'),
                    onChanged: (value) {
                      this.Title = value;
                    },
                  ),
                  SizedBox(height: 5.0),
                  TextField(
                    decoration: InputDecoration(hintText: 'Enter Summary'),
                    onChanged: (value) {
                      this.Summary = value;
                    },
                  ),
                ],
              ),
            ),
            actions: <Widget>[
              FlatButton(
                child: Text('Update'),
                textColor: Colors.blue,
                onPressed: () {
                  Navigator.of(context).pop();
                  crudObj.updateData(selectedDoc, {
                    'Title': this.Title,
                    'Summary': this.Summary
                  }).then((result) {
                    // dialogTrigger(context);
                  }).catchError((e) {
                    print(e);
                  });
                },
              )
            ],
          );
        });
  }

  Future<bool> dialogTrigger(BuildContext context) async {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Job Done', style: TextStyle(fontSize: 15.0)),
            content: Text('Added'),
            actions: <Widget>[
              FlatButton(
                child: Text('Alright'),
                textColor: Colors.blue,
                onPressed: () {
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        });
  }

  @override
  void initState() {
    crudObj.getData().then((results) {
      setState(() {
        tasks = results;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
          title: Text('Assigned Tasks'),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.add),
              onPressed: () {
                addDialog(context);
              },
            ),
            IconButton(
              icon: Icon(Icons.refresh),
              onPressed: () {
                crudObj.getData().then((results) {
                  setState(() {
                    tasks = results;
                  });
                });
              },
            )
          ],
        ),
        body: _TaskList(),
    );
  }

  Widget _TaskList() {
    if (tasks != null) {
      return StreamBuilder(
        stream: tasks,
        // ignore: missing_return
        builder: (context, snapshot) {
          if (snapshot.data != null) {
            return ListView.builder(
              itemCount: snapshot.data.documents.length,
              padding: EdgeInsets.all(7.0),
              itemBuilder: (context, i) {
                return  ListTile(
                  title: Text(snapshot.data.documents[i].data["Title"]??""),
                  subtitle: Text(snapshot.data.documents[i].data["Summary"]??""),
                  /*onTap: () {
*//*
                    updateDialog(
                        context, snapshot.data.documents[i].documentID);
                  },*/
                   onTap: (){
                Navigator.push(context,MaterialPageRoute(builder:(context)=>DetailPage(snapshot.data.douments[i])));

              },                     onLongPress: () {
                    crudObj.deleteData(snapshot.data.documents[i].documentID);
                  },
                );
              },
            );

          }
          return CircularProgressIndicator();
        },
      );
    } else {
      return Text('Loading, Please wait..');
    }
  }
}

当我按下 ontap 时,它应该转到详细页面,该页面仅显示该特定任务的详细信息(注意:我现在只有值 Title 和 summary,稍后我将添加其余部分,它们应该显示在详细信息页面中) 我的detailview如下

class DetailPage extends StatelessWidget {
  DocumentSnapshot detailDocument;
  DetailPage(this.detailDocument);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text( detailDocument.data["Title"]),
      ),
      body: Center(
        child: Text(detailDocument.data["Summary"]),
      ),
    );
  }
}

我收到一个错误 在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:

类 'QuerySnapshot' 没有实例 getter 'douments'。 接收方:“QuerySnapshot”实例 尝试调用:douments 相关的导致错误的小部件是: MaterialApp file:///C:/Users/TRICON/AndroidStudioProjects/taskmanager/lib/main.dart:21:14 抛出异常时,这是堆栈:

0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)

1 _assignedTaskState._TaskList....(包:taskmanager/screens/assignedtask.dart:208:106)

【问题讨论】:

    标签: flutter


    【解决方案1】:

    你正在使用

    Navigator.push(context,MaterialPageRoute(builder:(context)=>DetailPage(snapshot.data.douments[i])));
    

    而不是

    Navigator.push(context,MaterialPageRoute(builder:(context)=>DetailPage(snapshot.data.documents[i])));
    

    只是douments和documents的拼写不同而已。

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 2021-03-01
      • 2020-04-07
      • 2022-06-15
      • 1970-01-01
      • 2021-05-04
      • 2020-04-11
      • 2021-11-19
      相关资源
      最近更新 更多