The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'. Why I am getting this error in my listview builder?


before puting "!", I was getting this error:

The method ‘[]’ can’t be unconditionally invoked because the receiver
can be ‘null’.

This is my code:

                  future: getCity(),
                  builder: ((context, snapshot) {
                    if (snapshot.hasData) {
                      var DocData =;

                      return ListView.builder(itemBuilder: ((context, index) {
                        final String? title = DocData["Cities"];
                        return Text("testing");
                    return Text('Loading...');

  Future getCity() async {
    var doc = await FirebaseFirestore.instance

and this is my firestore database structure:

enter image description here

Data I am getting on Print call:

{Cities: {Islamabad: [{Price: 100000, Title: Apartment}, {Price: 200000, Title: House}], Mirpur: [{Price: 300000, Title: House}]}}

I want to show this data in my listview builder


use ! after checking null.

 if (snapshot.hasData) {
   var DocData = as Map?;  
    return ListView.builder(itemBuilder: ((context, index) {
     final String? title = DocData?["Cities"];
     return Text("testing ${title}");

Answered By – Yeasin Sheikh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published