google / google/reflectable.dart

How to pass empty lists to invokeSetter without knowing the list type?

Open
#349 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
386
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Hello.

I’m having trouble using `invokeSetter` with typed lists.

Consider the following code:

```dart
class Reflector extends Reflectable {
const Reflector() : super(instanceInvokeCapability);
}

const reflector = Reflector();

@reflector
class Person {
String? name;
int? age;
List

? addresses;
}

@reflector
class Address {
String street = '';
String city = '';
}

void main() {
initializeReflectable();

Person p = Person();

final im = reflector.reflect(p);
im.invokeSetter('addresses', []);
}
```

The problem is that when I call `im.invokeSetter('addresses', [])`, the value is interpreted as `List`, but the property actually expects a `List

`.
If I pass an explicitly typed list (`
[]`), it works. However, in my case I **don’t know the `Address` type at compile time** — it’s only discovered via reflection.

**My question is:**
Is there a way to:

1. Retrieve the expected type (`List

`) from the mirror?
2. Create or cast an empty list to that expected type, without having to write `
[]` explicitly?

In other words, I’d like to be able to instantiate the correct empty list with the reflected type, so that `invokeSetter` doesn’t throw a type error.

Contributor guide

Open the contributing guide

Research direction

The issue names invokeSetter and mirrors but provides no source file or test location. Start by tracing the invokeSetter API and the mirror type information it exposes, then determine whether a reflected empty typed list can be created without compile-time knowledge of Address. Done means documenting a supported approach or clearly establishing that the requested capability is not available.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.