Search code examples
androidkotlinfragmentdatastorepreference

How to use my datastore preference in fragment kotlin


private val Context.dataStore by preferencesDataStore("app_preferences")

I want to use the datastore above but the variable cannot be read or used in the fragment. like this picture enter image description here


Solution

  • To get dataStore reference in Fragment

    private val Context.dataStore by preferencesDataStore("app_preferences")
    
    
    // in Fragment
    val pref = requireContext().dataStore