Home > Mobile >  Problem with Kotlin Duration in Android Studio
Problem with Kotlin Duration in Android Studio

Time:07-29

I'm new with Android Studio and Kotlin and I have some trouble with Kotlin Duration. When I try to do following in my code:

val test: Duration = Duration.parse(value = timeClose)

I get this error message:

This declaration needs opt-in. Its usage must be marked with '@kotlin.time.ExperimentalTime' or '@OptIn(kotlin.time.ExperimentalTime::class)'

I use Android Studio Chipmunk 2021.2.1 Patch 1 with Kotlin 212-1.7.10

How can I solve my problem?

CodePudding user response:

Do what the error is telling you to do: mark it with one of the suggestions.

@kotlin.time.ExperimentalTime
val test: Duration = Duration.parse(value = timeClose)

or

@OptIn(kotlin.time.ExperimentalTime::class)
val test: Duration = Duration.parse(value = timeClose)

The reason you have to do this is that the Duration class is in the ExperimentalTime package, which means that this is an experimental preview, as the documentation states:

Note that this API is in a preview state and has a very high chance of being changed in the future. Do not use it if you develop a library since your library will become binary incompatible with the future versions of the standard library.

  • Related