Dagger Hilt Multibindings can not support List<T> type?

Viewed 37

I have a problem with Hilt by using it provides List example, the erroe message as below:

[Dagger/MissingBinding] java.util.List<? extends okhttp3.Interceptor> cannot be provided without an @Provides-annotated method.

so,what can i do for this?shoud i change it to Set,but i want a sorted array..

1 Answers

Multibindings do not support Lists, only Sets and Maps. In Dagger modules are not assumed to be included in a particular order, especially because they are de-duplicated within a component.

Let's say you want Module1 to provide Interceptor1 and Module2 to provide Interceptor2, and you want them installed in order. Even if Dagger were to guarantee alphabetic sorting and depth-first traversal, if ModuleB includes Module1 and Module2 and then you add ModuleA that depends on Module2, then regardless of ModuleB's ordering Module2 would be installed before Module1 is.

If you want an ordered list, you'll need to install it as a whole List you provide at once, or you'll need to sort the contents yourself. One technique for this is using Map Multibindings where you install a sortable key ("001 initial interceptor", "101 second layer", "201 third layer") etc and then traverse the Map in order of sorted key: this should give you the installability and order without relying on module loading order.

Related