New: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'DocumentSnapshot'

喜夏-厌秋 提交于 2021-01-04 09:27:15

问题


Sorry I am now in programming, but I an learning do please help me. I am stuck at this issue. This is one of my first app, and allmost done so need at likke help to get it done.

I am getting this error: type '_InternalLinkedHashMap' is not a subtype of type 'DocumentSnapshot'

The code looks like this:

    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:


Use this code:

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


来源:https://stackoverflow.com/questions/59130622/new-type-internallinkedhashmapstring-dynamic-is-not-a-subtype-of-type-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!