skip to Main Content

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


  1. 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.

    import 'package:flutter/material.dart';
        
        void main() {
          runApp(MyApp());
        }
        
        class CommentModel{
          final String text;
          final String user;
          final String time;
          const CommentModel({
            required this.text,
            required this.user,
            required this.time,
          });
        
        }
        
        class MyApp extends StatefulWidget {
          const MyApp({super.key});
        
          @override
          State<MyApp> createState() => _MyAppState();
        }
        
        class _MyAppState extends State<MyApp> {
          final List<CommentModel> comments = [
            CommentModel(
              text: "This is a comment",
              user: "User1",
              time: "12:00",
            ),
            CommentModel(
              text: "This is another comment",
              user: "User2",
              time: "12:01",
            ),
          ];
          @override
          Widget build(BuildContext context) {
            return MaterialApp(
              home: Scaffold(
                body: ListView(
                  shrinkWrap: true, //for nested lists
                  physics: const NeverScrollableScrollPhysics(),
                  children:comments.map((doc) {
                    //get the comment
                    final commentData = doc;
                    //return the comment
                    return Comment(
                      text: commentData.text,
                      user: commentData.user,
                      time: commentData.time,
                    );
                  }).toList(),
                ),
              ),
            );
          }
        }
        
        
        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),
                    ],
                  ),
                ],
              ),
            );
          }
        }
    
    Login or Signup to reply.
  2. Try to use listview of comment

    return ListView<Comment>(
      ..
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search