Skip to content

ConstantReader#peek #214

@matanlurey

Description

@matanlurey

In contrast with read, peek should return a null value instead of throwing an exception if a field is not found. This allows a more graceful way of checking for this first non-null value, for example:

findTokenField(DartObject o) {
  final reader = new ConstantReader(o);
  final token = o.peek('token') ?? o.peek('_token');
}

Other options:

  1. Add an Function<Object> orElse parameter to read.
  2. Add an explicit way to read/check many parameters, i.e.
final token = o.read('token', '_token'); 

// or

final token = o.readFirst(['token', '_token']);

Metadata

Metadata

Assignees

Labels

type-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions