新:类型“_InternalLinkedHashMap<String, dynamic>”不是“DocumentSnapshot”类型的子类型
Posted
技术标签:
【中文标题】新:类型“_InternalLinkedHashMap<String, dynamic>”不是“DocumentSnapshot”类型的子类型【英文标题】:New: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'DocumentSnapshot' 【发布时间】:2020-03-26 13:49:40 【问题描述】:对不起,我现在正在编程,但我正在学习,请帮助我。我被困在这个问题上。这是我的第一个应用程序之一,几乎完成了,所以需要 likeke 的帮助才能完成。
我收到此错误:类型“_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】:使用此代码:
snapshot.data[index].data // is of type Map<String,Dynamic>
snapshot.data[index] // is the DocumentSnapshot
【讨论】:
欢迎接受答案或投票赞成:D以上是关于新:类型“_InternalLinkedHashMap<String, dynamic>”不是“DocumentSnapshot”类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章