【问题标题】:3 positional argument(s) expected, but 2 found预期 3 个位置参数,但找到了 2 个
【发布时间】:2021-06-14 03:33:33
【问题描述】:

请帮忙。代码是使用用户 ID 从一个屏幕移动到另一个屏幕。这是一个聊天应用程序。

我收到的错误是:预期 3 个位置参数,但找到了 2 个。

我试图查看问题出在哪里,但似乎找不到。

这里是代码。

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

}

这是聊天类代码。

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

【问题讨论】:

  • 请加Chat班级代码
  • 完成。我已经添加了 ekstra 代码。

标签: flutter dart


【解决方案1】:

这里你必须传递 3 个参数:

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

字符串我的ID;

String selectedUserID;

字符串聊天ID;

【讨论】:

    【解决方案2】:

    查看Chat的构造函数:

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

    如您所见,共有三个参数。但现在让我们检查一下您的实例化:

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

    你能发现缺少的东西吗?第三个参数,chatID

    【讨论】:

      猜你喜欢
      • 2022-07-25
      • 2019-11-03
      • 2021-05-22
      • 2020-06-15
      • 2021-04-18
      • 2021-12-18
      • 2021-07-27
      • 1970-01-01
      • 2013-07-04
      相关资源
      最近更新 更多