Closure call with mismatched arguments: function '[]' in flutter

前端 未结 3 856
甜味超标
甜味超标 2021-01-25 12:12

** I am getting this error**

Closure call with mismatched arguments: function \'[]\'
Receiver: Closure: (dynamic) => dynamic from Function \'get\':.
Tried call         


        
相关标签:
3条回答
  • 2021-01-25 12:39

    You need to do the following:

    itemCount : snapshort.data.docs.length ,
    itemBuilder : (context,index){ 
      return QuizTile(url:snapshort.data.docs[index].data()['url'],
          title:snapshort.data.docs[index].data()['title'] ,
          desc: snapshort.data.docs[index].data()['desc'],
       );
      }
    );
    

    Since you are reference a collection, then you need to use docs which will retrieve a list of documents inside that collection:

    https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/query_snapshot.dart#L18

    Then to access each field in the document, you need to call data()

    0 讨论(0)
  • 2021-01-25 12:57

    The answer by @Peter Haddad is correct. Just to highlight the difference with an example from my own code:

    The previous version of code which created the same error:

    snapshot.data.docs[index].data["chatRoomID"]
    

    Updated version of code which solved the error:

    snapshot.data.docs[index].data()["chatRoomID"]
    
    0 讨论(0)
  • 2021-01-25 12:57

    Updated Version:

    snapshot.data[i]['Email'],
    
    Future getRequests() async {
        QuerySnapshot snapshot = await FirebaseFirestore.instance.collection("Buyer Requests").get();
        return snapshot.docs;
      }
    
    body: FutureBuilder(
              initialData: [],
              future: getRequests(),
              builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                indexLength = snapshot.data.length;
                if (snapshot.hasData)
                  return SizedBox(
                    child: PageView.builder(
                      itemCount: indexLength,
                      controller: PageController(viewportFraction: 1.0),
                      onPageChanged: (int index) => setState(() => _index = index),
                      itemBuilder: (_, i) {
                        return SingleChildScrollView(
                          child: Card(
                            margin: EdgeInsets.all(10),
                            child: Wrap(
                              children: <Widget>[
                                ListTile(
                                  leading: CircleAvatar(
                                    backgroundImage: AssetImage(
                                        'assets/images/shafiqueimg.jpeg'),
                                  ),
                                  title: Text(
                                    snapshot.data[i]['Email'],
                                    style: TextStyle(
                                      fontSize: 16,
                                      fontWeight: FontWeight.w700,
                                      color: Colors.black.withOpacity(0.7),
                                    ),
                                  ),
                                  subtitle: Text(
                                    snapshot.data[i]['Time'],
                                    style: TextStyle(
                                        color: Colors.black.withOpacity(0.6)),
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.all(16.0),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.stretch,
                                    children: [
                                      Container(
                                        decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.all(Radius.circular(5)),
                                          color: Colors.grey[200],
                                        ),
                                        padding: EdgeInsets.all(10),
                                        child: Text(
                                          snapshot.data[i]['Description'],
                                          style: TextStyle(
                                              color: Colors.black.withOpacity(0.6)),
                                        ),
                                      ),
                                      SizedBox(
                                        height: 8,
                                      ),
                                      Container(
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(5)),
                                            border: Border.all(
                                                color: Colors.grey[300])),
                                        child: ListTile(
                                          leading: Icon(Icons.category_outlined),
                                          title: Text(
                                            'Category : ${snapshot.data[i]['Category']}',
                                            style: TextStyle(
                                              fontSize: 14,
                                              color: Colors.grey,
                                            ),
                                          ),
                                        ),
                                      ),
                                       SizedBox(height: 8),
                                      Container(
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(5)),
                                            border: Border.all(
                                                color: Colors.grey[300])),
                                        child: ListTile(
                                          leading: Icon(Icons.location_pin),
                                          title: Text(
                                            snapshot.data[i]['Location'],
                                            style: TextStyle(
                                              fontSize: 14,
                                              color: Colors.grey,
                                            ),
                                          ),
                                        ),
                                      ),
                                      SizedBox(height: 8),
                                      Container(
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(5)),
                                            border: Border.all(
                                                color: Colors.grey[300])),
                                        child: ListTile(
                                          leading: Icon(
                                            Icons.attach_money,
                                            color: kGreenColor,
                                          ),
                                          title: Text(
                                            'Rs.${snapshot.data[i]['Budget']}',
                                            style: TextStyle(
                                              fontSize: 14,
                                              color: kGreenColor,
                                            ),
                                          ),
                                        ),
                                      ),
                                      SizedBox(height: 8),
                                      Container(
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(5)),
                                            border: Border.all(
                                                color: Colors.grey[300])),
                                        child: ListTile(
                                          leading: Icon(Icons.timer),
                                          title: Text(
                                            'Duration : ${snapshot.data[i]['Duration']}',
                                            style: TextStyle(
                                              fontSize: 14,
                                              color: Colors.grey,
                                            ),
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        height: 35,
                                      ),
                                      RaisedButton(
                                        padding: EdgeInsets.symmetric(vertical: 10),
                                        child: Text('Send Offer'),
                                        textColor: Colors.white,
                                        color: Colors.green,
                                        onPressed: () {
                                          // Respond to button press
                                        },
                                      ),
                                      SizedBox(
                                        height: 15,
                                      ),
                                      Center(
                                        child: Text(
                                          "${i + 1}/$indexLength",
                                          style: TextStyle(fontSize: 13),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  );
                else
                  return Center(
                    child: Text("Null"),
                  );
              },
            ),
    
    0 讨论(0)
提交回复
热议问题