Flutter 应为3个位置参数,但找到2个
请帮忙。代码是使用userID从一个屏幕移动到另一个屏幕。这是一个聊天应用程序 我收到的错误是:应为3个位置参数,但找到2个 我试图找出问题所在,但似乎找不到 这是代码Flutter 应为3个位置参数,但找到2个,flutter,dart,Flutter,Dart,请帮忙。代码是使用userID从一个屏幕移动到另一个屏幕。这是一个聊天应用程序 我收到的错误是:应为3个位置参数,但找到2个 我试图找出问题所在,但似乎找不到 这是代码 Future<void> _moveToChat(selectedUserID) async { try { String chatID; SharedPreferences prefs = await SharedPreferences.getInstance(); String myID = (pre
Future<void> _moveToChat(selectedUserID) async {
try {
String chatID;
SharedPreferences prefs = await SharedPreferences.getInstance();
String myID = (prefs.get('userID') ?? 'userID');
if(myID.hashCode > selectedUserID.hashCode) {
chatID = '${selectedUserID.hashCode} - ${myID.hashCode}';
}else{
chatID = '${myID.hashCode} - ${selectedUserID.hashCode}';
}
FirebaseFirestore.instance.collection('chat').doc(chatID).set({});
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Chat(myID, selectedUserID))
);
}catch(e){
print(e.message);
}
Future\u移动到帽子(selectedUserID)异步{
试一试{
字符串chatID;
SharedReferences prefs=等待SharedReferences.getInstance();
字符串myID=(prefs.get('userID')??'userID');
如果(myID.hashCode>selectedUserID.hashCode){
chatID='${selectedUserID.hashCode}-${myID.hashCode}';
}否则{
chatID='${myID.hashCode}-${selectedUserID.hashCode}';
}
FirebaseFirestore.instance.collection('chat').doc(chatID).set({});
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>Chat(myID,selectedUserID))
);
}捕获(e){
打印(电子信息);
}
}
这是聊天类代码
class Chat extends StatefulWidget {
Chat(this.myID, this.selectedUserID, this.chatID);
String myID;
String selectedUserID;
String chatID;
@override
_ChatState createState() => _ChatState();
}
class _ChatState extends State<Chat> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Container(
height: 60.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.scaleDown,
image: AssetImage('assets/images/logo.png'),
),
),
),
),
body: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('Chats')
.orderBy('createdAt', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Container(
child: Center(
child: CircularProgressIndicator(),
),
color: kAccentColor,
);
return Text('dd');
},
),
);
}
}
类聊天扩展StatefulWidget{
聊天室(this.myID、this.selectedUserID、this.chatID);
字符串myID;
字符串selectedUserID;
字符串chatID;
@凌驾
_ChatState createState()=>\u ChatState();
}
类_ChatState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:对,
标题:集装箱(
身高:60.0,
装饰:盒子装饰(
图像:装饰图像(
适合度:BoxFit.scaleDown,
图像:AssetImage('assets/images/logo.png'),
),
),
),
),
正文:StreamBuilder(
流:FirebaseFirestore.instance
.collection(“聊天室”)
.orderBy('createdAt',降序:true)
.snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)返回容器(
儿童:中心(
子对象:CircularProgressIndicator(),
),
颜色:kAccentColor,
);
返回文本('dd');
},
),
);
}
}
这里您必须传递3个参数:
MaterialPageRoute(builder: (context) => Chat(myID, selectedUserID));
字符串myID
字符串selectedUserID
字符串chatID 检查
聊天
的构造函数:
Chat(this.myID、this.selectedUserID、this.chatID)
如你所见,有三个论点。但现在让我们检查一下您的实例化:
MaterialPage路线(生成器:(上下文)=>Chat(myID,selectedUserID))
你能发现丢失了什么吗?第三个参数,
chatID
Add请Chat
classcodedone。我已经添加了ekstra代码。非常感谢!成功了!非常感谢你!成功了!