简体   繁体   English

预期 3 个位置参数,但找到了 2 个

[英]3 positional argument(s) expected, but 2 found

Please help.请帮忙。 The code is to move from one screen to another with userID.代码是使用用户 ID 从一个屏幕移动到另一个屏幕。 This is for an chat app.这是一个聊天应用程序。

Error i receive is:3 positional argument(s) expected, but 2 found.我收到的错误是:预期 3 个位置参数,但找到了 2 个。

I've tried to see where the problem is but can not seem to find it.我试图看看问题出在哪里,但似乎找不到。

Here is the code.这是代码。

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

} }

Here is the Chat class code.这是聊天 class 代码。

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

Here you have to pass 3 arguments:在这里你必须通过 3 arguments:

    MaterialPageRoute(builder: (context) => Chat(myID, selectedUserID));

String myID;字符串我的ID;

String selectedUserID;字符串选择用户ID;

String chatID;字符串聊天ID; <- this one is not passed to Chat class as third positional argument. <- 这个没有作为第三个位置参数传递给 Chat class。

Check the constructor of Chat :检查Chat的构造函数:

Chat(this.myID, this.selectedUserID, this.chatID)

As you can see, there are three arguments.如您所见,共有三个 arguments。 But now let's check your instantiation:但现在让我们检查您的实例化:

MaterialPageRoute(builder: (context) => Chat(myID, selectedUserID))

Can you spot what's missing?你能发现缺少什么吗? The third argument, chatID .第三个参数, chatID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM