Closure call with mismatched arguments: function '[]' error is being shown in flutter

Viewed 1607

In my program, this error comes in my laptop only. I typed the exact code as of the tutorial, but in tutorial, it is working fine, and i am getting this error -

Closure call with mismatched arguments: function '[]'
Receiver: Closure: () => Map<String, dynamic> from Function 'data':.
Tried calling: []("message")
Found: []() => Map<String, dynamic>

This is my code, in which error is coming. If you want full details of functions, you can comment it out, and i will provide you the same in comments.

import 'package:flutter/material.dart';
import 'package:whatsapp/helper/constants.dart';
import 'package:whatsapp/services/database.dart';
import 'package:whatsapp/widget/widgets.dart';

class ConversationScreen extends StatefulWidget {

  final String chatRoomId;
  ConversationScreen(this.chatRoomId);

  @override
  _ConversationScreenState createState() => _ConversationScreenState();
}

class _ConversationScreenState extends State<ConversationScreen> {

  DatabaseMethods databaseMethods = new DatabaseMethods();
  TextEditingController messageController = new TextEditingController();
  Stream chatMessageStream;

  Widget ChatMessageList() {
    return StreamBuilder(
        stream: chatMessageStream,
      builder: (context, snapshot){
          return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) {
              return MessageTile(snapshot.data.documents[index].data["message"]);
            },
          );
      },
    );
  }

  sendMessage() {
    if(messageController.text.isNotEmpty) {
      Map<String, String> messageMap = {
        "message" : messageController.text,
        "sender" : Constants.myName,
      };
      databaseMethods.addConversationMessages(widget.chatRoomId, messageMap);
      messageController.text = "";
    }
  }

  @override
  void initState() {
    databaseMethods.getConversationMessages(widget.chatRoomId).then((value){
      setState(() {
        chatMessageStream = value;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Color(0xff1f1f1f),
        appBar: appBarMain(context),
        body: Container(
          child: Stack(
            children: [
              ChatMessageList(),
              Container(
                alignment: Alignment.bottomCenter,
                child: Container(
                  alignment: Alignment.bottomCenter,
                  padding: EdgeInsets.symmetric(horizontal: 5.0, vertical: 10.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Expanded(
                        child: Container(
                          width: 300.0,
                          padding: EdgeInsets.symmetric(horizontal: 10.0),
                          decoration: BoxDecoration(
                            color: Color(0xff2D3D3C),
                            borderRadius: BorderRadius.circular(50.0),
                          ),
                          child: TextField(
                            cursorColor: Color(0xff246e5d),
                            controller: messageController,
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 15.0,
                            ),
                            decoration: InputDecoration(
                              border: InputBorder.none,
                              focusedBorder: InputBorder.none,
                              enabledBorder: InputBorder.none,
                              errorBorder: InputBorder.none,
                              disabledBorder: InputBorder.none,
                              hintText: 'Type a message',
                              hintStyle: TextStyle(
                                color: Colors.white54,
                              ),
                            ),
                          ),
                        ),
                      ),
                      GestureDetector(
                        onTap: (){
                          sendMessage();
                        },
                        child: Padding(
                          padding: EdgeInsets.fromLTRB(8.0, 3.0, 1.0, 2.0),
                          child: Container(
                            height: 50.0,
                            width: 50.0,
                            decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              color: Color(0xff246e5d),
                            ),
                            child: Center(
                              child: Icon(
                                Icons.send,
                                color: Colors.white54,
                                size: 30.0,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class MessageTile extends StatelessWidget {

  final String message;
  MessageTile(this.message);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(
        message,
        style: TextStyle(
          color: Colors.white,
        ),
      ),
    );
  }
}
1 Answers

DocumentSnapshot.data() is a method not a getter, therefore it's necessary to include the parentheses to actually call the method and obtain the returned Map. This is why dart is confused and throws an error as you're trying to use the [] operator on a function reference.

return MessageTile(snapshot.data.documents[index].data()["message"]);
Related