'The following _CastError was thrown building NoteAdder(dirty, state: _NoteAdder#76214):Null check operator used on a null value

The error (given in the title) was thrown when I ran the app.

Here is my code

class NoteAdder extends StatefulWidget {

  @override
  _NoteAdder createState() => _NoteAdder();
}

class _NoteAdder extends State<NoteAdder> {
  Note? note;

  TextEditingController titleController = TextEditingController();
  TextEditingController descriptionController = TextEditingController();

  @override
  Widget build(BuildContext context) {

    titleController.text = note!.title!;
    descriptionController.text = note!.description!;


    return AlertDialog(
        backgroundColor: Colors.lime,
        content: Column(
          children: [
            const Text(
              'ADD NOTE',
              style: TextStyle(fontSize: 25),
            ),

            const SizedBox(height: 30),

            Container(
              alignment: Alignment.topLeft,
              child: const Text('Title:'),
            ),

            TextField(
              controller: titleController,
              decoration: InputDecoration(
                border: UnderlineInputBorder(),
              ),
            ),

            const SizedBox(height: 30),

            Container(
              alignment: Alignment.topLeft,
              child: const Text('Description:'),
            ),

            TextField(
              controller: descriptionController,
              maxLines: 13,
              decoration: InputDecoration(
                border: UnderlineInputBorder(),
              ),
            ),

            const SizedBox(height: 35),

            Container(
                alignment: Alignment.center,
                child: ElevatedButton(
                    style: ElevatedButton.styleFrom(primary: Colors.red),
                    onPressed: () {
                      setState(() {
                        save();
                      });
                    },
                    child: const Text('Save')))
          ],
        ));
  }

  void save() async {
    note?.date = DateFormat.yMMMd().format(DateTime.now()) as DateTime?;
    if (note?.id != null) {
      await NoteDatabaseHelper.update(note!)??0;
    } else {
      await NoteDatabaseHelper.insert(note!)??0;
    }
  }

}

I am a bit new to flutter. Please help me to solve this problem

Link to my complete project: https://github.com/SayanBanerjee09082002/Daily_Utility

Note: The add screen appears when I press a floating action button. The app runs ok until I hit that button.



Solution 1:[1]

Since you wrote Note? note;, note == null so trying to use it with null check like this note?.date = DateFormat.yMMMd().format(DateTime.now()) as DateTime?; will throw error. Now I don't know what the constructor of your class Note look like so my answer may not be accurate; but as answer, I will advice you to do either:

Note? note = Note(); //I don't know the structure of the constructor, so you have to deal with that part

or inside save()

if(note != null) {
   note.date = DateFormat.yMMMd().format(DateTime.now()) as DateTime;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fsbelinda