简体   繁体   中英

3 positional argument(s) expected, but 2 found

Please help. The code is to move from one screen to another with userID. This is for an chat app.

Error i receive is:3 positional argument(s) expected, but 2 found.

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 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:

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

String myID;

String selectedUserID;

String chatID; <- this one is not passed to Chat class as third positional argument.

Check the constructor of Chat :

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

As you can see, there are three arguments. But now let's check your instantiation:

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

Can you spot what's missing? The third argument, chatID .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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