I can't set field to NULL on flutter firestore

546

The new Cloud Firestore library cannot store null values because of the new null safety feature. I would recommend you use a keyword, like an empty space or intricate piece of string as your custom null variable which should work Declare

String newNull = 'IntricateNull';

Pass to Firestore

'nullValue':newNull

Use it when receiving data

if (value != newNull){//To check value is not null
}

Its not a favorable solution but it is what it is. Null safety has its downsides and upsides.

Share:
546
sanderi sihvo
Author by

sanderi sihvo

Updated on December 28, 2022

Comments

  • sanderi sihvo
    sanderi sihvo over 1 year

    I use JSON serialization to upload documents to firestore.

    I just updated my flutter project to nullsafety and now i get this error on any NULL field:

    Error: [cloud_firestore/unknown] Expected a value of type 'Object', but got one of type 'Null'

    My fields in class are nullabe:

    String? field;

    Is this a bug or am i doing something wrong? I was able to have null fields before this update. I use web build

  • sanderi sihvo
    sanderi sihvo over 3 years
    You are right, storing null values seems to be are out of the question. My solution was to use JsonKey like so: @JsonKey(includeIfNull: false) String? field;
  • Siddharth Agrawal
    Siddharth Agrawal over 3 years
    Its your choice. I assume it should work but you should check that once