Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with Kotlin Duration in Android Studio

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)’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

How can I solve my problem?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading