title: Android | Kotlin | Flow - cannot be cast to kotlinx.coroutines.flow.StateFlow
body: <p>I'm trying to get myself familiar with <code>DataStore</code>, so in my current project, I'm trying to use it.</p>
  <p>In my dependency. I've added :</p>
  <pre><code>    implementation &quot;androidx.datastore:datastore-preferences:1.0.0-alpha06&quot;
  </code></pre>
  <p>Then I created this class to handle data-store:</p>
  <pre><code>class BasicDataStore(context: Context) :
      PrefsDataStore(
          context,
          PREF_FILE_BASIC
      ),
      BasicImpl {
      override val serviceRunning: Flow&lt;Boolean&gt;
          get() = dataStore.data.map { preferences -&gt;
              preferences[SERVICE_RUNNING_KEY] ?: false
          }
      override suspend fun setServiceRunningToStore(serviceRunning: Boolean) {
          dataStore.edit { preferences -&gt;
              preferences[SERVICE_RUNNING_KEY] = serviceRunning
          }
      }
      companion object {
          private const val PREF_FILE_BASIC = &quot;basic_preference&quot;
          private val SERVICE_RUNNING_KEY = booleanPreferencesKey(&quot;service_running&quot;)
      }
  }
  @Singleton
  interface BasicImpl {
      val serviceRunning: Flow&lt;Boolean&gt;
      suspend fun setServiceRunningToStore(serviceRunning: Boolean)
  }
  </code></pre>
  <p>In my viewmodel, I'm trying to use that value, like this :</p>
  <pre><code>class MainViewModel(application: Application) : AndroidViewModel(application) {
      ...
      private val basicDataStore = BasicDataStore(application)
      val serviceRunning
              : StateFlow&lt;Boolean&gt; get()
              = basicDataStore.serviceRunning as StateFlow&lt;Boolean&gt;
      fun setServiceRunning(serviceRunning: Boolean) {
          viewModelScope.launch(IO) {
              basicDataStore.setServiceRunningToStore(serviceRunning)
          }
      }
  }
  </code></pre>
  <p>But it gives me the following error :</p>
  <pre><code>Caused by: java.lang.ClassCastException: com.mua.roti.data.datastore.BasicDataStore$serviceRunning$$inlined$map$1 cannot be cast to kotlinx.coroutines.flow.StateFlow
          at com.mua.roti.viewmodel.MainViewModel.getServiceRunning(MainViewModel.kt:33)
  ...
  </code></pre>
  <p>In xml, in UI part :</p>
  <pre><code>
          &lt;TextView
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:text=&quot;@{main.serviceRunning.value.toString()}&quot; /&gt;
  
  </code></pre>
  <p>With viewmodel everything was so cool and easy, easy to read and implement. Now I'm confused with Flow. Thanks.</p>