posts.dart file
return ListView(
shrinkWrap: true, //for nested lists
physics: const NeverScrollableScrollPhysics(),
children: snapshot.data!.docs.map((doc) {
//get the comment
final commentData = doc.data() as Map<String, dynamic>;
//return the comment
return Comment(
text: commentData["CommentText"],
user: commentData["CommentedBy"],
time: formatDate(commentData["CommentTime"]),
);
}).toList(),
);
comment.dart file
import 'package:flutter/material.dart';
class Comment extends StatelessWidget {
final String text;
final String user;
final String time;
const Comment({
super.key,
required this.text,
required this.user,
required this.time,
});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(4),
),
child: Column(
children: [
//comment
Text(text),
//user time
Row(
children: [
Text(user),
Text(" . "),
Text(time),
],
),
],
),
);
}
}
Comment()
class is not getting recognized by the ListView
and to make it work I even put toList()
method at the end of the bracket but it still gives the error I have written in the title.
You can also take a look at the comment.dart file which describes the Comment method. I’m confused as to why isn’t it exporting in the main file.
2
Answers
Could you provide detailed error messages and screenshots of the errors? Otherwise, it’s challenging for me to determine the issue you’re encountering. I’ve implemented a test case based on the code snippets you provided, and I didn’t encounter the error you mentioned.
Try to use listview of comment