flutterdartjson-serializationbuilt-value

how to make a public property even with underscore in dart?


I'm fetching some json data from a rest api server and one of its keys is _id and I need to serialize this json to a dart object using built_value, but this isn't allowed because in dart _id is private and built_value doesn't allow me to define a private getter in my model!
So what do I do?


Solution

  • package:built_value has a mechanism to rename fields. As mentioned in its README.md:

    The corresponding dart class employing built_value might look like this. Note that it is using ... the @BuiltValueField annotation to map between the property name on the response and the name of the member variable in the Person class.

      ...
    
      @nullable
      @BuiltValueField(wireName: 'first_name')
      String get firstName;
    

    So in your case, you should be able to do something like:

    @BuiltValueField(wireName: '_id')
    String get id;