Reputation: 189
I got an error message when I was trying to build an app that contains chat, the message is:
type 'String' is not a subtype of type 'bool'
a screenshot of the error message:
class Messages extends StatelessWidget {
@override
Widget build(BuildContext context) {
User user2= FirebaseAuth.instance.currentUser;
return FutureBuilder(
// future: Future<String>.delayed(
// Duration(seconds: 2),
// () => 'Data Loaded',
// ),
//future: FirebaseAuth.instance.currentUser,
builder:(ctx, futureSnapshot){
if(futureSnapshot.connectionState==ConnectionState.waiting){
return Center(child: CircularProgressIndicator());}
return StreamBuilder(
stream: FirebaseFirestore.instance.collection('chat').orderBy('createdAt',descending: true).snapshots(),
builder: (ctx, chatSnapshot){
if(chatSnapshot.connectionState== ConnectionState.waiting){
return Center(child: CircularProgressIndicator(),);
}
if(chatSnapshot.hasData){
final chatDocs= chatSnapshot.data.docs;
return ListView.builder(
//key: ValueKey(chatDocs[index].docsID),
reverse: true,
itemCount:chatDocs.length,
itemBuilder: (ctx, index)=> MessageBubble(
chatDocs[index]['text'],
chatDocs[index]['userId'],
//chatDocs[index]['userId'] ==futureSnapshot.data.udi,
ValueKey(chatDocs[index].docsID),
)
);}
else{
return Text('عذرًا, هناك مشكلة');
}
},
);
},
);
}
Can anyone suggest a solution and tell me what should I do?
Upvotes: 0
Views: 454
Reputation: 3084
The problem is this line you commented here:
MessageBubble(
chatDocs[index]['text'],
chatDocs[index]['userId'],
// chatDocs[index]['userId'] ==futureSnapshot.data.udi,
ValueKey(chatDocs[index].docsID),
)
Why? Your Object MessageBubble has 4 positional arguments. The third one of them is isMe
and expects a boolean
. However, your passing the ValueKey (since they are positional).
Also, your not providing the 4º argument that is required
since all positional arguments are required
class MessageBubble extends StatelessWidget {
final String messsage;
final userID;
final bool isMe;
final Key key;
MessageBubble(this.messsage,this.userID, this.isMe,this.key);
You could:
chatDocs[index]['userId'] ==futureSnapshot.data.udi
or pass it another boolean value (true or false)Upvotes: 1
Reputation: 189
@MansiBhatt
MessageBubble class:
class MessageBubble extends StatelessWidget {
final String messsage;
final userID;
final bool isMe;
final Key key;
MessageBubble(this.messsage,this.userID, this.isMe,this.key);
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: isMe? MainAxisAlignment.start: MainAxisAlignment.end,
children: [
Container(
decoration: BoxDecoration(
color: isMe? Colors.grey[300]: nave,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
bottomLeft: !isMe ? Radius.circular(12) : Radius.circular(0),
bottomRight: isMe ? Radius.circular(12) : Radius.circular(0),
),
),
width: 140,
padding: EdgeInsets.symmetric(vertical: 10,horizontal: 16),
margin: EdgeInsets.symmetric(vertical: 4, horizontal: 8),
child: Column(
children: [
Column(
children: [
FutureBuilder(
future: FirebaseFirestore.instance.collection('users').doc(userID).get(),
builder: (context, snapshot) {
if(snapshot.connectionState== ConnectionState.waiting){
return Text('جاري التحميل...');}
return Text(snapshot.data['username'], style: TextStyle(fontWeight: FontWeight.bold),);
}
),
],
),
Text(messsage, style: TextStyle(color: isMe? white : Colors.black,),),
],
) ,
),
],
);
}
}
Upvotes: 0