Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject into var in Kotlin?

I try to implement Dagger2 into my Kotlin project but I have problems with the @Inject annotation.

In Java it looks like this and this works fine:

public class FooActivity extends Activity {

    @Inject
    @Named("accessTokenObservable")
    public Flowable<Optional<AccessToken>> accessTokenObservable;

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        App.getGraph().inject(this);
    }
}

But how I have to write the @Inject line in Kotlin?

When I use this one:

@Inject
@Named("accessTokenObservable")
var accessTokenObservable: Flowable<Optional<AccessToken>>? = null

I get this error message:

Error:Dagger does not support injection into private fields

If I use lateinit:

@Inject
@Named("accessTokenObservable")
lateinit var accessTokenObservable: Flowable<Optional<AccessToken>>

I get this error message:

Error:Flowable<Optional<AccessToken>> cannot be provided without
an @Provides- or @Produces-annotated method.

What is the right syntax to inject something in Kotlin?

like image 828
Ralph Bergmann Avatar asked Feb 26 '17 22:02

Ralph Bergmann


People also ask

How do you inject Kotlin?

The building block of kotlin-inject is a component which you declare with an @Component annotation on an abstract class. An implementation of this component will be generated for you. For external dependencies, you can declare a function or read-only property in the component to create an instance for a certain type.

What is get () in Kotlin?

In Kotlin, setter is used to set the value of any variable and getter is used to get the value. Getters and Setters are auto-generated in the code. Let's define a property 'name', in a class, 'Company'. The data type of 'name' is String and we shall initialize it with some default value.

Why do we use Lateinit in Kotlin?

The lateinit keyword allows you to avoid initializing a property when an object is constructed. If your property is referenced before being initialized, Kotlin throws an UninitializedPropertyAccessException , so be sure to initialize your property as soon as possible.

How do you define a class variable in Kotlin?

Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val . Use var for a variable whose value can change.


3 Answers

I just use

@Inject
lateinit var presenter: ItemsPresenter

override fun onCreate(savedInstanceState: Bundle?) {
    AndroidInjection.inject(this)
    super.onCreate(savedInstanceState)
    ...
}

Without any problems

like image 146
Tuby Avatar answered Oct 26 '22 18:10

Tuby


You have to change your injection code like this:

@field:[Inject Named("accessTokenObservable")]
lateinit var accessTokenObservable: Flowable<Optional<AccessToken>>

, and it will be fixed.

like image 34
Mohsen Mirhoseini Avatar answered Oct 26 '22 17:10

Mohsen Mirhoseini


Do you provide Flowable<Optional<AccessToken>> accessTokenObservable somewhere in your code?

If not Error:Flowable<Optional<AccessToken>> cannot be provided without an @Provides- or @Produces-annotated method. can be produced. Because you are trying to inject object without provide it. In this case you need to provide in your injector class:

@Provide
@Named("accessTokenObservable")
fun provideAccessTokenObservable : Flowable<Optional<AccessToken>>{
    return yourAccessTokenObservable
}

then you need to inject your object in your activity

@Inject
@Named("accessTokenObservable")
lateinit var accessTokenObservable: Flowable<Optional<AccessToken>>
like image 35
Ergin Ersoy Avatar answered Oct 26 '22 19:10

Ergin Ersoy