参数不匹配的闭包调用:flutter中显示函数'[]'错误 [英] Closure call with mismatched arguments: function '[]' error is being shown in flutter

查看:44
本文介绍了参数不匹配的闭包调用:flutter中显示函数'[]'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,此错误仅在笔记本电脑中出现.我在本教程中键入了准确的代码,但是在本教程中,它工作正常,并且出现此错误-

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,
        ),
      ),
    );
  }
}

推荐答案

DocumentSnapshot.data()是一种非获取方法,因此必须包含括号才能实际调用该方法并获得返回的 Map .这就是为什么在尝试在函数引用上使用 [] 运算符时dart感到困惑并引发错误的原因.

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"]);

这篇关于参数不匹配的闭包调用:flutter中显示函数'[]'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆