Is that anything wrong? BUt I had write a return statement
The Error
Widget _buildCommentList() {
return ListView.builder(itemBuilder: (context, index) {
if (index < _comments.length) {
return _buildCommentItem(_comments[index]);
}
});
}
The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type.Try adding either a return or a throw statement at the end.
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:penang_beachess/blog_page/blog_page.dart';
import 'package:flutter/material.dart';
import 'package:penang_beachess/models/user_model.dart';
import 'Post.dart';
class CommentPlugin extends StatefulWidget {
const CommentPlugin({Key? key}) : super(key: key);
@override
State<CommentPlugin> createState() => _CommentPluginState();
}
class _CommentPluginState extends State<CommentPlugin> {
List<String> _comments = [];
void _addComment(String val) {
setState(() {
_comments.add(val);
});
}
Widget _buildCommentList() {
return ListView.builder(itemBuilder: (context, index) {
if (index < _comments.length) {
return _buildCommentItem(_comments[index]);
}
});
}
Widget _buildCommentItem(String comment) {
return Card(
child: ListTile(
title: Text("${loggedInUser.username}"),
title: Text(comment),
),
);
}
User? user = FirebaseAuth.instance.currentUser;
UserModel loggedInUser = UserModel();
@override
void initState() {
super.initState();
FirebaseFirestore.instance
.collection("users")
.doc(user!.uid)
.get()
.then((value) {
this.loggedInUser = UserModel.fromMap(value.data());
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return Center(
child: IconButton(
icon: const Icon(
Icons.chat_bubble_outline,
color: Colors.grey,
size: 36,
),
onPressed: () {
showModalBottomSheet(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(25.0)),
),
backgroundColor: Colors.white,
context: context,
isScrollControlled: true,
builder: (context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
children: [
IconButton(
icon: Icon(Icons.arrow_back, color: Colors.blue),
onPressed: () {
Navigator.of(context).pop();
},
),
const Text('Back'),
],
),
Expanded(child: _buildCommentList()),
TextField(
onSubmitted: (String submitedStr) {
_addComment(submitedStr);
},
cursorColor: Colors.black,
maxLength: 20,
decoration: const InputDecoration(
filled: true,
icon: Icon(Icons.chat_bubble_outline),
contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
hintText: "Write a comment...",
),
),
// )),
],
);
});
},
));
}
}