Home > Mobile >  How to fix " Null check operator used on a null value" error ? (Flutter)
How to fix " Null check operator used on a null value" error ? (Flutter)

Time:07-14

I got some error on my code and I can't figure out how to exactly fix it. Everytime I click on "edit item" I get that error. It tells me also that: The following _CastError was thrown building FormBuilderField(dirty, dependencies: [_FormScope, UnmanagedRestorationScope], state: FormBuilderFieldState<FormBuilderField, int>#e0024): Null check operator used on a null value

The relevant error-causing widget was: FormBuilderField FormBuilderField:file:///lib/src/widgets/selected_ngo_widget.dart:23:12

Here the code for the selector:

class SelectedNgo extends StatefulWidget {
  int? id;
  String? name;
  String? logo;
  int? defaultValue;
  final GlobalKey<FormBuilderState> formKey;

  SelectedNgo(this.formKey, {this.defaultValue, this.name, this.logo, this.id, Key? key})
      : super(key: key);

  @override
  State<SelectedNgo> createState() => _SelectedNgoState();
}

class _SelectedNgoState extends State<SelectedNgo> {
  @override
  Widget build(BuildContext context) {
    return FormBuilderField(
        name: 'ngoId',
        initialValue: widget.id,
        builder: (FormFieldState<int> field) {
          return Form(
            child: Card(
              child: Row(
                children: [
                  const SizedBox(
                    width: 10,
                  ),
                  Padding(
                    padding: const EdgeInsets.all(10),
                    child: CachedNetworkImage(
                      imageUrl: '${widget.logo}_SMALL.jpg',
                      placeholder: (context, url) => const CircularProgressIndicator(),
                      errorWidget: (context, url, error) => const Icon(Icons.error),
                      height: 80,
                      fit: BoxFit.contain,
                    ),
                  ),
                  Expanded(
                      child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        widget.name!,
                        textAlign: TextAlign.center,
                        style: GoogleFonts.k2d(
                          textStyle: const TextStyle(
                            fontWeight: FontWeight.w600,
                            fontSize: 14,
                            color: Colors.black,
                          ),
                        ),
                      ),
                    ],
                  ))
                ],
              ),
            ),
          );
        });
  }
}

Thank you in advance for your help!

CodePudding user response:

Here widget.name must be null. You can add it in a string like this

Text('${widget.name!}')

But please note you are not passing the name to this widget . This will display null in place of name

CodePudding user response:

"Null check operator used on a null value" means you use ! on a variable that's null. In the shown code the only place where you use that is

widget.name!

Which means that name must be null. Somewhere you must be creating a SelectedNgo without passing a name or with passing null as name.

  • Related