Skip to content

Generated Dart code expects PbMap instead of Map which is hard to create #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gryznar opened this issue Apr 24, 2025 · 2 comments · Fixed by #976
Closed

Generated Dart code expects PbMap instead of Map which is hard to create #975

gryznar opened this issue Apr 24, 2025 · 2 comments · Fixed by #976

Comments

@gryznar
Copy link

gryznar commented Apr 24, 2025

Previously it was:

class Foo extends $pb.GeneratedMessage {
  factory Foo({
    $core.Map<$core.String, Bar>? bars,
  }) {
    final $result = create();
    if (bars!= null) {
      $result.bars.addAll(bars);
    }
    return $result;
  }
  ...
}

now it is:

class Foo extends $pb.GeneratedMessage {
  factory Foo({
    $pb.PbMap<$core.String, Bar>? bars,
  }) {
    final $result = create();
    if (bars!= null) {
      $result.bars.addAll(bars);
    }
    return $result;
  }
  ...
}

Creating PbMap manually is very problematic, as it expects 2 ints (one for key and one for value). Personally I don't know how to obtain them. Situation is much simpler with PbList which just exposes .from(List<E> from) constructor

@mraleph
Copy link
Member

mraleph commented Apr 25, 2025

I think we should probably either under the #903 change partially or - if narrower type actually leads to some benefits - we should provide a convenient way to create these.

@osa1
Copy link
Member

osa1 commented May 1, 2025

Good catch -- the factory argument type does not need to be a PbMap or even a Map, it could be any iterable that yields key-value pairs with the right types.

This change was done a while ago and I don't remember the details, but it must've been unintentional. I changed how the field is represented and that effected factory argument types as well. We can and should distinguish those two.

We can make the argument type Iterable<MapEntry<K, V>>.

For now, as a work around, you can create an empty message and then add the map elements with message.mapField.addAll or message.mapField[key] = value.

osa1 added a commit to osa1/protobuf.dart that referenced this issue May 1, 2025
osa1 added a commit that referenced this issue May 7, 2025
@osa1 osa1 closed this as completed in #976 May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants