Home > database >  The argument type 'List<ChatModel>' can't be assigned to the parameter type �
The argument type 'List<ChatModel>' can't be assigned to the parameter type �

Time:10-01

I'm a student and beginner to dart. I'm creating a simple chat functionality. I got solution to all my errors here, but not this one. I found some similar issue like mine, but they didn't help.

Expanded(
  child: ListView.builder(
    itemCount: widget.chatmodels.length,
    itemBuilder: (contex, index) =>
    ChatTile(chatModel:widget.chatmodels ), // I got the error here
  ),
),

The error is: The argument type 'List<ChatModel>' can't be assigned to the parameter type 'ChatModel'.

CodePudding user response:

You forgot to add the index, so the cycle goes through each item. Just add [index]:

Expanded(
  child: ListView.builder(
    itemCount: widget.chatmodels.length,
    itemBuilder: (context, index) =>
      ChatTile(chatModel:widget.chatmodels[index]), // Need to tell it to go to the specific item of the chat models
  ),
),

CodePudding user response:

Try to access each instance of your chatmodels using the index key which is available inside itemBuilder like below:

 Expanded(
    child: ListView.builder(
        itemCount: widget.chatmodels.length,
        itemBuilder: (contex, index) =>
            ChatTile(chatModel:widget.chatmodels[index]),
            ),
  ),
  • Related