Home > front end >  How to avoid "null check operator used null value" Flutter
How to avoid "null check operator used null value" Flutter

Time:10-17

when I go to this page I see this error (Null check operator used on a null value) instead of the content.

I have read questions that had the same error and in the answers they talked about one ! too many, but I could not figure out where the problem is in my code, perhaps the error refers to something else? Can you tell me hot to correct the code, please?

This is my home controller: https://pastecode.io/s/g0frr7ui

class PastTasksView extends GetView<HomeController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: Get.height,
        width: Get.width,
        color: Theme.of(context).scaffoldBackgroundColor,
        //padding: EdgeInsets.symmetric(horizontal: 30, vertical: 50),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.only(top: 50, left: 25, right: 25),
              child: Text(
                'Attività svolte',
                style: kSubHeadTextStyle.copyWith(
                    color: Theme.of(context).primaryColorDark),
              ),
            ),
            SizedBox(height: Get.height * 0.012),
            GetBuilder<HomeController>(
              id: 1,
              builder: (controller) {
                return Expanded(
                  child: ListView.builder(
                    itemBuilder: (context, index) {
                      final task = controller.pastTasks[index]!;
                      return Slidable(
                        // actionPane: SlidableBehindActionPane(),
                        // actionExtentRatio: 0.2,
                        // controller: controller.slideC,
                        child: ExpandedContainer(
                          icon: task.taskImage,
                          title: task.tasktitle,
                          time: task.startTime,
                          desc: task.taskDesc,
                          ifDate: true,
                          date: DateFormat.yMMMd().format(task.taskDate!),
                        ),
                        startActionPane: ActionPane(
                          motion: BehindMotion(),
                          children: [
                            SlidableAction(
                              onPressed: (context) {
                                // controller.slideC.activeState?.close();
                                Slidable.of(context)?.close();
                                controller.preUpdateTask(task);
                                showModalBottomSheet(
                                  backgroundColor: Colors.transparent,
                                  isScrollControlled: true,
                                  context: context,
                                  builder: (context) {
                                    return BottomSheetContent(
                                      buttonText: 'Update Task',
                                      onSubmit: () {
                                        controller.updateTask(task);
                                      },
                                    );
                                  },
                                ) as IconData;
                              },
                              label: "Update",
                            ),
                          ],
                        ),
                        endActionPane: ActionPane(
                          motion: BehindMotion(),
                          children: [
                            SlidableAction(
                              onPressed: (context) {
                                // controller.slideC.activeState?.close();
                                Slidable.of(context)?.close();
                                controller.deleteTask(task);
                              },
                              label: "Delete",
                            ),
                          ],
                        ),
                      );
                    },
                    itemCount: controller.pastTasks.length,
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

The error tells you that you force-unwrap a null-value via the !-operator. This is forbidden. So you would have to search your code for occurences of this operator. I see the following lines that might be the source of the error.

final task = controller.pastTasks[index]!;

and

date: DateFormat.yMMMd().format(task.taskDate!),

In both cases, you would have to check the value for null first.

CodePudding user response:

There are few places you are using !, It would be better to do a null check 1st then perform action,

Like

  final task = controller.pastTasks[index];
  if( task==null ) return Text("got Null value");
   
  //else 
  return Slidable(....

Same approach goes for

task.taskDate!=null? DateFormat.yMMMd().format(task.taskDate): yourDefaultDateFormat,

Find more about understanding-null-safety

  • Related