I want to add likes to comments in the application I am writing. In my codes, when I click on the icon in Firebase, I want it to open a field called like and write uid. When I do this in my posts, I succeed, but not in the comments. My codes are as follows. What should I do.
ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
I get the error.
Future<void> LikeComment(String postId, String uid, List likes, String commentId) async {
try {
if (likes.contains(uid)) {
await _firestore.collection('posts').doc(postId).collection('comments').doc(commentId).update({
'likes': FieldValue.arrayRemove([uid]),
});
} else {
await _firestore.collection('posts').doc(postId).collection('comments').doc(commentId).update({
'likes': FieldValue.arrayUnion([uid]),
});
}
} catch (e) {
print(
e.toString(),
);
}
}
like
Row(
children: [
LikeAnimation(
isAnimating: widget.snap['likes']!=null && widget.snap['likes'].contains(myUid),
smallLike: true,
child: IconButton(
onPressed: () async {
await FirestoreMethods().LikeComment(
widget.snap['postId'],
myUid,
widget.snap['commentId'],
widget.snap['likes'],
);
},
icon: widget.snap['likes']!=null && widget.snap['likes'].contains(myUid)
? const Icon(
Icons.favorite,
color: Colors.red,
)
: const Icon(
Icons.favorite_border,
),
),
),
]),
CodePudding user response:
With the information you have given, you need to check your parameters for null values (Debug your code).
Future<void> LikeComment(String postId, String uid, List likes, String commentId) async {
print(postId); print(uid); print(likes); print(commentId); // check for the one that returns null and find a way to handle it
//highlight and comment all your below code.
}
CodePudding user response:
I am refering to make variables nullable and return/handle on null cases.
Future<void> LikeComment(
String? postId, String? uid, List? likes, String? commentId) async {
if (postId == null || uid == null || likes == null || commentId == null) {
print("postId $postId uid $uid likes $likes commentId $commentId ");
return;
}