Home > Mobile >  Flutter: Bad state: field does not exist within the DocumentSnapshotPlatform in streambuilder
Flutter: Bad state: field does not exist within the DocumentSnapshotPlatform in streambuilder

Time:09-24

getting this error showing in red screen i update my packages but again this error is showing

The following StateError was thrown building StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Map<String, dynamic>>, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>>#eea86): Bad state: field does not exist within the DocumentSnapshotPlatform

The relevant error-causing widget was: StreamBuilder<QuerySnapshot<Map<String, dynamic>>> StreamBuilder:file:///C:/Users/flutt/Desktop/New folder/MarcJr-main/Logisyntax-MarcJr-master/lib/pages/Ecommerce/SellerDashboard/SoldItemScreen.dart:34:20

When the exception was thrown, this was the stack: #0 DocumentSnapshotPlatform.get._findKeyValueInMap (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:87:7) #1 DocumentSnapshotPlatform.get._findComponent (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:105:23) #2 DocumentSnapshotPlatform.get (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:121:12) #3 _JsonDocumentSnapshot.get (package:cloud_firestore/src/document_snapshot.dart:92:48) #4 _JsonDocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:96:40) #5 _SoldItemScreensState.build.. (package:marcjrfoundation/pages/Ecommerce/SellerDashboard/SoldItemScreen.dart:63:39) #6 MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31) #7 ListIterator.moveNext (dart:_internal/iterable.dart:342:26) #8 new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27) #9 new _GrowableList.of (dart:core-patch/growable_array.dart:150:28) #10 new List.of (dart:core-patch/array_patch.dart:51:28) #11 ListIterable.toList (dart:_internal/iterable.dart:213:44) #12 _SoldItemScreensState.build. (package:marcjrfoundation/pages/Ecommerce/SellerDashboard/SoldItemScreen.dart:89:22) #13 StreamBuilder.build (package:flutter/src/widgets/async.dart:444:81) #14 _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48) #15 StatefulElement.build (package:flutter/src/widgets/framework.dart:4992:27) #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4878:15) #17 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11) #18 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5) #19 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2667:19)

 import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:marcjrfoundation/services/SharedPreferences/sharedprefs_helper.dart';
import 'package:marcjrfoundation/services/device_size.dart';
import 'package:websafe_svg/websafe_svg.dart';

import '../ProductItem/sellerProductItem.dart';

class SoldItemScreens extends StatefulWidget {
  static final String tag = '/soldItemScreen';
  @override
  _SoldItemScreensState createState() => _SoldItemScreensState();
}

class _SoldItemScreensState extends State<SoldItemScreens> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      appBar: AppBar(
        title: Text(
          'Sold Products',
          style: TextStyle(
              fontSize: ResponsiveWidget.isSmallScreen(context) ? 17.0 : 25.0),
        ),
      ),
      body: Column(
        children: [
          SizedBox(
            height: 10.0,
          ),
          Container(
            color: Color(0xffF7F7F7),
            child: StreamBuilder(
              stream: FirebaseFirestore.instance
                  .collection("items")
                  .where('seller', isEqualTo: parentIdGlobal)
                  .where('isSold', isEqualTo: true)
                  .snapshots(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (!snapshot.hasData)
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                List<DocumentSnapshot<Object>> docs = snapshot.data!.docs;
                if (docs.length == 0) {
                  return Center(
                    child: Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: WebsafeSvg.asset(
                        'assets/images/sold-out.svg',
                        fit: BoxFit.cover,
                        height: MediaQuery.of(context).size.height / 5,
                      ),
                    ),
                  );
                }
                return ListView(
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  children: docs.map((DocumentSnapshot doc) {
                    bool isSold = doc['isSold'];
                    bool isLiked = doc['isLiked'];
                    String itemId = doc['itemId'];
                    String seller = doc['seller'];
                    String sellerName = doc['sellerName'];
                    String title = doc['title'];
                    String desc = doc['desc'];
                    String price = doc['price'];
                    String condition = doc['condition'];
                    String category = doc['category'];
                    String location = doc['location'];
                    String itemImage = doc['imageDownloadUrl'];

                    return SellerProductItem(
                      itemId: itemId,
                      seller: seller,
                      sellerName: sellerName,
                      title: title,
                      desc: desc,
                      price: price,
                      itemImage: itemImage,
                      isLiked: isLiked,
                      isSold: isSold,
                      category: category,
                      condition: condition,
                      location: location,
                    );
                  }).toList(),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

CodePudding user response:

According to the current version(3.4.8) of the cloud_firestore,

There is a problem in below line of your code.

List<DocumentSnapshot<Object>> docs = snapshot.data!.docs;

Datatype that you have selected is not correct. snapshot.data?.docs is of type List<QueryDocumentSnapshot<Map<String, dynamic>>>?.

You can replace streamBuilder in your code with below code snippet.

StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection("items")
            .where('seller', isEqualTo: parentIdGlobal)
            .where('isSold', isEqualTo: true)
            .snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return CircularProgressIndicator();
          }
          final List<QueryDocumentSnapshot<Map<String, dynamic>>> docSnapList =
              snapshot.data?.docs ?? [];

          if (docSnapList.isEmpty) {
            return Center(
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: WebsafeSvg.asset(
                  'assets/images/sold-out.svg',
                  fit: BoxFit.cover,
                  height: MediaQuery.of(context).size.height / 5,
                ),
              ),
            );
          }
          final List<Map<String, dynamic>> docList = docSnapList.map((QueryDocumentSnapshot<Map<String, dynamic>> queryDocumentSnapshot) => queryDocumentSnapshot.data()).toList();
          return ListView.builder(
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            itemCount: docList.length,
            itemBuilder: (context, index) {
              bool isSold = docList[index]['isSold'];
              bool isLiked = docList[index]['isLiked'];
              String itemId = docList[index]['itemId'];
              String seller = docList[index]['seller'];
              String sellerName = docList[index]['sellerName'];
              String title = docList[index]['title'];
              String desc = docList[index]['desc'];
              String price = docList[index]['price'];
              String condition = docList[index]['condition'];
              String category = docList[index]['category'];
              String location = docList[index]['location'];
              String itemImage = docList[index]['imageDownloadUrl'];

              return SellerProductItem(
                itemId: itemId,
                seller: seller,
                sellerName: sellerName,
                title: title,
                desc: desc,
                price: price,
                itemImage: itemImage,
                isLiked: isLiked,
                isSold: isSold,
                category: category,
                condition: condition,
                location: location,
              );
            },
          );
        },
      );
  • Related