Home > Mobile >  FormKey makes error that Null check operator used on a null value
FormKey makes error that Null check operator used on a null value

Time:06-21

I'm making Instagram with Flutter and I'm adding a comment function under the feed item. However, when I push the comment submit button, the error "Null check operator used on a null value" comes out.

I'm using a formKey to check whether it's valid to submit or not. How can I use it properly and how can I fix this problem? Thank you!

class CommentInput extends StatefulWidget {
  const CommentInput({Key? key, required this.snapshot, required this.postId})
      : super(key: key);

  final snapshot;
  final postId;
  @override
  State<CommentInput> createState() => _CommentInputState();
}

class _CommentInputState extends State<CommentInput> {
  final _commentController = TextEditingController();
  final _formKey = GlobalKey<FormState>();

  @override
  void dispose() {
    super.dispose();
    _commentController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      child: Container(
        height: 55.0,
        margin: const EdgeInsets.symmetric(horizontal: 8.0),
        child: Row(
          children: [
            Flexible(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12.0),
                child: TextFormField(
                  validator: (input) {
                    if (input == null) {
                      return "Please enter comment";
                    }
                  },
                  controller: _commentController,
                  decoration: const InputDecoration(
                    hintText: "Add a comment...",
                  ),
                ),
              ),
            ),
            GestureDetector(
              child: Container(
                margin: const EdgeInsets.only(right: 8.0),
                child: const Text('Post', style: TextStyle(color: Colors.blue)),
              ),
              onTap: () {
                final isValid = _formKey.currentState!.validate(); //Error Null check operator used on a null value
                if (isValid) {
                  _formKey.currentState!.save();
                  context.read<HomeData>().uploadComment(
                      widget.snapshot.uid,
                      widget.postId,
                      _commentController.text,
                      widget.snapshot.username,
                      widget.snapshot.photoUrl);
                }
              },
            )
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

Hey you haven’t provide your from key to Form widget yet --

i have updated your code check it -

 @override
  Widget build(BuildContext context) {
    return Form(
      key:_formKey,
      child: Container(
        height: 55.0,
        margin: const EdgeInsets.symmetric(horizontal: 8.0),
        child: Row(
          children: [
            Flexible(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12.0),
                child: TextFormField(
                  validator: (input) {
                    if (input == null) {
                      return "Please enter comment";
                    }
                  },
                  controller: _commentController,
                  decoration: const InputDecoration(
                    hintText: "Add a comment...",
                  ),
                ),
              ),
            ),
            GestureDetector(
              child: Container(
                margin: const EdgeInsets.only(right: 8.0),
                child: const Text('Post', style: TextStyle(color: Colors.blue)),
              ),
              onTap: () {
                final isValid = _formKey.currentState!.validate(); //Error Null check operator used on a null value
                if (isValid) {
                  _formKey.currentState!.save();
                  context.read<HomeData>().uploadComment(
                      widget.snapshot.uid,
                      widget.postId,
                      _commentController.text,
                      widget.snapshot.username,
                      widget.snapshot.photoUrl);
                }
              },
            )
          ],
        ),
      ),
    );
  }
  • Related