1

I'm trying to get data from the firebase database. I'm getting this error:

The operator '[]' isn't defined for the class 'Object'

Try many methods but the error is still here.

StreamBuilder<QuerySnapshot>(
            stream: _categoryProvider.category.snapshots(),
              builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
              if(snapshot.hasError){
                return Text('Something went wrong..');
              }
              if(snapshot.connectionState == ConnectionState.waiting){
                return Center(child: CircularProgressIndicator(),);
              }
              return Expanded(
                child: ListView(
                  children: snapshot.data.docs.map((DocumentSnapshot document){
                      return ListTile(
                        leading: CircleAvatar(
                          backgroundImage: NetworkImage(document?.data()['image']),// error is on this line
                        ),
                        //title: Text(document.data()['category name']),
                        onTap: (){},
                      );
                    }).toList(),
                ),
              );
              }),

Here is CategoryProvider.

import 'package:cloud_firestore/cloud_firestore.dart';

class CategoryProvider{
  CollectionReference category = FirebaseFirestore.instance.collection('categories');
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Adeel Nazim
  • 640
  • 6
  • 17
  • 1
    I think the closing parentheses in in the wrong place`NetworkImage(document?.data())['image']` – Educorreia Aug 23 '21 at 17:25
  • Hy thanks for your time and answer. Now this is the error ```lib/widgets/category_list.dart:56:74: Error: The operator '[]' isn't defined for the class 'NetworkImage'. - 'NetworkImage' is from 'package:flutter/src/painting/image_provider.dart' ('/C:/flutter/packages/flutter/lib/src/painting/image_provider.dart'). Try correcting the operator to an existing operator, or defining a '[]' operator. backgroundImage: NetworkImage(document?.data())['image'], ``` – Adeel Nazim Aug 23 '21 at 17:31

2 Answers2

1

I guess you confused QueryDocumentSnapshot with DocumentSnapshot. This is because there are two different snapshots() methods (one from the Query class and one from the DocumentReference class).

Change this:

children: snapshot.data.docs.map((DocumentSnapshot document){

to this:

children: snapshot.data.docs.map((QueryDocumentSnapshot document) {
Stewie Griffin
  • 4,690
  • 23
  • 42
0

use this instead:

final temp = document.data; // once try this and if it did not work use json.decode(document.data)
return Expanded(
                child: ListView(
                  children: snapshot.data.docs.map((DocumentSnapshot document){
                      return ListTile(
                        leading: CircleAvatar(
                          backgroundImage: NetworkImage(temp['image']),// error is on this line
                        ),
                        //title: Text(temp['category name']),
                        onTap: (){},
                      );
                    }).toList(),
                ),

let me know the result.

Abbasihsn
  • 2,075
  • 1
  • 7
  • 17