''Object? Function()?' can't be assigned to the parameter type 'Map<String, dynamic>

I am trying to retrieve product data from firestore to display on my homepage, but am unable to proceed further as encountered with this squiggly line shouting.

What am I doing wrong? Appreciate it and thank you in advance.

body: CustomScrollView( slivers: [ SliverPersistentHeader(pinned: true, delegate: SearchBoxDelegate()),

      // Retrieve and display products in the storehome page
      StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance
            .collection('items')
            .limit(15)
            .orderBy('publishedDate', descending: true)
            .snapshots(),
        builder: (context, dataSnapshot) {
          return !dataSnapshot.hasData
              ? SliverToBoxAdapter(
                  child: Center(
                    child: circularProgress(),
                  ),
                )
              : SliverStaggeredGrid.countBuilder(
                  crossAxisCount: 1,
                  staggeredTileBuilder: (c) => StaggeredTile.fit(1),
                  itemBuilder: (context, index) {
                    ItemModel model = ItemModel.fromJson(
                        dataSnapshot.data?.docs[index].data);
                    return sourceInfo(model, context);
                  },
                  itemCount: dataSnapshot.data!.docs.length,
                );
        },
      ),
    ],
  ),

enter image description here


Error:

W/Firestore(10923): (24.0.0) [WatchStream]: (bfdc3) Stream closed with status: Status{code=CANCELLED, description=Disconnecting idle stream. Timed out waiting for new targets., cause=null}.

lib/Store/storeHome.dart:109:42: Error: The argument type 'Object? Function()?' can't be assigned to the parameter type 'Map<String, dynamic>'.

  • 'Object' is from 'dart:core'.
  • 'Map' is from 'dart:core'. dataSnapshot.data?.docs[index].data);

My model

import 'package:cloud_firestore/cloud_firestore.dart';

class ItemModel {
  String? title;
  String? shortInfo;
  Timestamp? publishedDate;
  String? thumbnailUrl;
  String? longDescription;
  String? status;
  int? price;

  ItemModel(
      {required this.title,
      required this.shortInfo,
      required this.publishedDate,
      required this.thumbnailUrl,
      required this.longDescription,
      required this.status,
      required this.price});


  ItemModel.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    shortInfo = json['shortInfo'];
    publishedDate = json['publishedDate'];
    thumbnailUrl = json['thumbnailUrl'];
    longDescription = json['longDescription'];
    status = json['status'];
    price = json['price'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['title'] = this.title;
    data['shortInfo'] = this.shortInfo;
    if (this.publishedDate != null) {
      data['publishedDate'] = this.publishedDate;
    }
    data['thumbnailUrl'] = this.thumbnailUrl;
    data['longDescription'] = this.longDescription;
    data['status'] = this.status;
    data['price'] = this.price;

    return data;
  }
}

//
class PublishedDate {
  String? date;

  PublishedDate({required this.date});

  PublishedDate.fromJson(Map<String, dynamic> json) {
    date = json['$date'];
  }

  //
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['$date'] = this.date;
    return data;
  }
}
                                     ^


Solution 1:[1]

It had to cast the method call to Map.

ItemModel model = ItemModel.fromJson(
                          dataSnapshot.data!.docs[index].data()!
                              as Map<String, dynamic>,
                        );

Solution 2:[2]

dataSnapshot.data?.docs[index].data

is a method. If you want the result, you need to call it. I also added a ! instead of a ? because you already did then when finding out how many items you will have:

dataSnapshot.data!.docs[index].data()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikash Deka
Solution 2 nvoigt