Home > Blockchain >  The Flutter Error show that " A non-null value must be returned since the return type 'Wid
The Flutter Error show that " A non-null value must be returned since the return type 'Wid

Time:06-28

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...",
                     
                    ),
                  ),
                  // )),
                ],
              );
            });
      },
    ));
  }
}

CodePudding user response:

The issue is the _buildCommentList return widget only when it meet the if condition index < _comments.length. While the widget is not nullable it cant return null.

You can make return nullable widget and check null while using it. or use else statement to return a(default) widget from this method.

Widget? _buildCommentList() {
    return ListView.builder(itemBuilder: (context, index) {
       if (index < _comments.length) {
      return _buildCommentItem(_comments[index]);
       }
    });
  }

and use case if(_buildCommentList()!=null) Expanded(child: _buildCommentList()!),

Visit flutter.dev to learn more about null-safety.

  • Related