nullable_absent 2.0.0 copy "nullable_absent: ^2.0.0" to clipboard
nullable_absent: ^2.0.0 copied to clipboard

Library provides a simple class that represents `absent` value.

Absent value for nullable field

Features #

Simple wrapper class for representing absent value. You might need it when you create copyWith method for class that contains nullable field.

Getting started #

Pubspec #

dependencies:
  nullable_absent: ^2.0.0

Usage #

For class that contains nullable field and you want to have copyWith function.

class MyData {
  final String id;
  final String? name;
  
  MyData({required this.id, required this.name});
}

For non-null field, it is obvious that we can use String? for specifying new value for id, or just leave it null to use the old value But for nullable field, we sometimes want to set it to null, and we sometimes want to use the old value. So String? is not enough.

MyData copyWith({String? id}) {
  return MyData(
      id: id ?? this.id, 
      name: name ?? this.name, // What if we want to set name = null
  );
}

With nullable_absent, we can distinguish between set to null or use old value like this.

MyData copyWith({
    String? id,
    NullableAbsent<String> name = const NullableAbsent.absent(),
}) {
  return MyData(
      id: id ?? this.id, 
      name: NullableAbsent(this.name).apply(name),
  );
}

apply(newValue) is a convenient method for creating the result value.

Additional information #

You can use typedef to make code shorter.

typedef $<T> = NullableAbsent<T>

// copyWith code
MyData copyWith({
  String? id,
  $<String> name = const $.absent(),
}) {
  return MyData(
      id: id ?? this.id, 
      name: $(this.name).apply(name));
}

// Usage
final dataWithNewName = data.copyWith(name: $("new_name"));
1
likes
160
points
573
downloads

Publisher

unverified uploader

Weekly Downloads

Library provides a simple class that represents `absent` value.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on nullable_absent