新增:类型'_InternalLinkedHashMap <String,dynamic>'不是类型'DocumentSnapshot'的子类型

时间:2019-12-01 22:37:38

标签: firebase listview flutter dart

对不起,我现在正在编程中,但是我正在学习中,请帮助我。我被困在这个问题上。这是我的第一个应用程序,在需要帮助的情况下,所有操作都已完成。

我收到此错误:类型'_InternalLinkedHashMap'不是类型'DocumentSnapshot'的子类型

代码如下:

    import 'package:flutter/material.dart';
    import 'package:brew_app/services/auth.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';

    class Home extends StatelessWidget {

    final AuthService _auth = AuthService();
    @override
    Widget build(BuildContext context) {
     return Scaffold(
      backgroundColor : Colors.grey[50],
      appBar: AppBar(
        title: Text('Brew Crew'),
        backgroundColor: Colors.orange[400],
        elevation: 0.0,
        actions: <Widget>[
          FlatButton.icon(
            icon: Icon(Icons.person),
            label: Text('Logout'),
            onPressed:() async {
              await _auth.signOut();

           },

         )
       ],
     ),
     body: ListPage(),
   );
 }

}

  class ListPage extends StatefulWidget {



    @override
    _ListPageState createState() => _ListPageState();
  }

  class _ListPageState extends State<ListPage> {

 Future getPosts() async {
   var firestore = Firestore.instance;

QuerySnapshot qn = await firestore.collection("posts").getDocuments();

return qn.documents;


  }

  navigateToDetails(DocumentSnapshot post){
    Navigator.push(context, MaterialPageRoute(builder: (context) => DetailPage(post: post,)));
 } 

 @override
  Widget build(BuildContext context) {
    return Container(
     child: FutureBuilder(
       future: getPosts(),
         builder: (_, snapshot){
        if(snapshot.connectionState == ConnectionState.waiting) {
          return Center(
            child: Text("Loading..."),
          );
        } else {
          return ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (_, index){

                return ListTile(
                  title: Text(snapshot.data[index].data["title"]),
                  onTap: () => navigateToDetails(snapshot.data[index].data),
                );

          });


    }

  }),
);

} }

class DetailPage extends StatefulWidget {


  final DocumentSnapshot post;

  DetailPage({this.post});
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Card(
        child: ListTile(
          title: Text(widget.post.data["title"]),
          subtitle: Text(widget.post.data["content"]),
        )
      )
    );
  }
}

1 个答案:

答案 0 :(得分:0)

使用此代码:

snapshot.data[index].data // is of type Map<String,Dynamic>     
snapshot.data[index] // is the DocumentSnapshot