gpt4 book ai didi

firebase - 参数不匹配的闭包调用 : function '[]' error is being shown in flutter

转载 作者:行者123 更新时间:2023-12-03 04:12:41 26 4
gpt4 key购买 nike

在我的程序中,此错误仅出现在我的笔记本电脑中。我输入了教程中的确切代码,但在教程中,它工作正常,但我收到此错误 -

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

这是我的代码,其中出现错误。如果您想了解功能的完整详细信息,您可以将其注释掉,我会在评论中为您提供相同的内容。

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() 是一个方法而不是 getter,因此需要包含括号才能实际调用该方法并获取返回的 Map。这就是当您尝试在函数引用上使用 [] 运算符时 dart 感到困惑并抛出错误的原因。

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

关于firebase - 参数不匹配的闭包调用 : function '[]' error is being shown in flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64309170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com