首页 > 解决方案 > 错误状态:flutter 2.5 上的 DocumentSnapshotPlatform 中不存在字段

问题描述

嗨,我正在尝试从 firebase 获取数据并将其显示到 listView 中,但出现错误如何解决?这是我的代码错误出现在streambuilder上我认为从我发布的内容看来我正试图从a中获取一个字段。子集合,我确保文档 ID 是正确的以及它的字段名称来拉它们。

class TimelineScreen extends StatefulWidget {
  TimelineScreen({Key? key}) : super(key: key);

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

class _TimelineScreenState extends State<TimelineScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          color: Theme.of(context).primaryColor,
          borderRadius: BorderRadius.only(
            bottomLeft: Radius.circular(10),
            bottomRight: Radius.circular(10),
          ),
        ),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.only(left: 15.0, top: 20, right: 10),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          "# Top popularité",
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey,
                          ),
                        ),
                        IconButton(
                          icon: Icon(Icons.leaderboard_rounded,
                              color: Colors.grey),
                          onPressed: () {},
                        )
                      ],
                    ),
                    Container(
                      height: 85,
                      margin: EdgeInsets.only(
                        top: 8,
                        left: 16,
                        bottom: 8,
                      ),
                      child: ListView.builder(
                          scrollDirection: Axis.horizontal,
                          itemCount: data_post.length,
                          itemBuilder: (context, index) {
                            return Container(
                              width: 65,
                              margin: EdgeInsets.only(left: 16),
                              child: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Stack(
                                    children: [
                                      Container(
                                        height: 60,
                                        width: 60,
                                        padding: EdgeInsets.all(2),
                                        decoration: BoxDecoration(
                                            image: DecorationImage(
                                                fit: BoxFit.cover,
                                                image: AssetImage(
                                                  data_post[index]['profilPic'],
                                                )),
                                            borderRadius: BorderRadius.all(
                                              Radius.circular(30),
                                            ),
                                            color:
                                                Theme.of(context).primaryColor,
                                            border: Border.all(
                                                width: 3,
                                                color:
                                                    Colors.deepPurpleAccent)),
                                      ),
                                      Positioned(
                                        bottom: 0,
                                        right: 0,
                                        child: Container(
                                          height: 18,
                                          width: 18,
                                          decoration: BoxDecoration(
                                              borderRadius: BorderRadius.all(
                                                Radius.circular(9),
                                              ),
                                              color: Colors.green,
                                              border: Border.all(
                                                  color: Theme.of(context)
                                                      .primaryColor,
                                                  width: 2)),
                                        ),
                                      )
                                    ],
                                  ),
                                  Text(data_post[index]['username'],
                                      style: TextStyle(
                                          fontSize: 12,
                                          fontFamily: 'Robotto',
                                          fontWeight: FontWeight.w400,
                                          color: Theme.of(context).accentColor))
                                ],
                              ),
                            );
                          }),
                    )
                  ],
                ),
              ),
              Divider(),
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: Text(
                  "# Actualité",
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: StreamBuilder<QuerySnapshot>(
                  stream: FirebaseFirestore.instance
                      .collection(POST_COLLECTION)
                      .snapshots(),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Center(
                          child: SpinKitDoubleBounce(
                              color: Colors.deepPurpleAccent, size: 50));
                    } else {
                      return loadPosts(context, snapshot);
                    }
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget loadPosts(
      BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    return Wrap(
      spacing: 10,
      runSpacing: 10,
      children: [
        ListView(
            children: snapshot.data!.docs.map((documentSnapshot) {
          return GestureDetector(
            onTap: () {
              Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                return TimelineInfo(
                    imagepost: documentSnapshot.get('photoUrl'));
              }));
            },
            child: Container(
              width: (MediaQuery.of(context).size.width - 30) / 2,
              height: 250,
              child: Stack(
                children: [
                  Container(
                      width: (MediaQuery.of(context).size.width - 30) / 2,
                      height: 250,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Hero(
                          tag: 'location-img-${"url"}',
                          child: Image.network(documentSnapshot.get('photoUrl'),
                              fit: BoxFit.cover))),
                  Container(
                    width: (MediaQuery.of(context).size.width - 15) / 2,
                    height: 250,
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                          colors: [
                            Colors.black.withOpacity(0.3),
                            Colors.black.withOpacity(0)
                          ],
                          end: Alignment.topCenter,
                          begin: Alignment.bottomCenter),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        ListTile(
                          title: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Icon(Icons.star, color: Colors.white, size: 12),
                              SizedBox(
                                width: 3,
                              ),
                              Text("",
                                  style: TextStyle(
                                      fontFamily: 'Robotto',
                                      color: Colors.white,
                                      fontSize: 12))
                            ],
                          ),
                          subtitle: Padding(
                            padding: const EdgeInsets.only(left: 3.0),
                            child: Text("12",
                                style: TextStyle(
                                    color: Colors.white.withOpacity(0.7),
                                    fontSize: 10,
                                    fontWeight: FontWeight.bold)),
                          ),
                          trailing: GestureDetector(
                            onTap: () {
                              void showBottom() => showModalBottomSheet(
                                  enableDrag: true,
                                  isDismissible: true,
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.only(
                                        topLeft: Radius.circular(30),
                                        topRight: Radius.circular(20)),
                                  ),
                                  context: context,
                                  builder: (context) => Column(
                                        mainAxisSize: MainAxisSize.min,
                                        children: [
                                          SizedBox(height: 10),
                                          HandleWidget(),
                                          ListTile(
                                            leading: Stack(
                                              children: [
                                                CircleAvatar(
                                                  radius: 40,
                                                  backgroundColor:
                                                      Theme.of(context)
                                                          .accentColor,
                                                  backgroundImage: NetworkImage(
                                                      documentSnapshot
                                                          .get('photoUrl')),
                                                ),
                                                Positioned(
                                                    bottom: 0,
                                                    right: 12,
                                                    child: CircleAvatar(
                                                      radius: 7,
                                                      backgroundColor:
                                                          Theme.of(context)
                                                              .primaryColor,
                                                      child: CircleAvatar(
                                                        radius: 5,
                                                        backgroundColor:
                                                            Colors.red,
                                                      ),
                                                    ))
                                              ],
                                            ),
                                            title: Text(
                                              "username",
                                              style: TextStyle(
                                                  fontSize: 20,
                                                  fontFamily: 'Robotto'),
                                            ),
                                            subtitle: Row(
                                              children: [
                                                Icon(Feather.map_pin, size: 12),
                                                SizedBox(width: 5),
                                                Text("region"),
                                              ],
                                            ),
                                            trailing: IconButton(
                                                onPressed: () {},
                                                icon: Icon(Feather.user)),
                                          ),
                                          Divider(),
                                          UserStats(
                                              likes: "30",
                                              friends: "30",
                                              popularite: "30"),
                                          Divider(),
                                        ],
                                      ));
                              showBottom();
                            },
                            child: CircleAvatar(
                              radius: 20,
                              backgroundColor: Theme.of(context).accentColor,
                              backgroundImage: NetworkImage(
                                  documentSnapshot.get('photoUrl')),
                            ),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          );
        }).toList()),
      ],
    );
  }
}

 String postId = "${userId}_${name}";
      Map<String, dynamic> postData = {
        "type": "video",
        "description": description,
        "uid": userId,
        "time": Timestamp.now(),
        "postUrl": uploadPostVideoUrl,
        "postId": postId,
      };
      await FirebaseFirestore.instance
          .collection(POST_COLLECTION)
          .doc(postId)
          .set(postData, SetOptions(merge: true));

这是错误

════════ Exception caught by widgets library ═══════════════════════════════════
Bad state: field does not exist within the DocumentSnapshotPlatform
The relevant error-causing widget was
StreamBuilder<QuerySnapshot<Object?>> StreamBuilder:file:///home/***/Documents/flutter/***/***/lib/mainScreen/bottomNavigation/timeline.dart:145:24
════════════════════════════════════════════════════════════════════════════════

标签: androidflutterdarterror-handlingcustom-error-handling

解决方案


推荐阅读