# com.google.firebase.datastorage

## Extension functions summary

|---|---|
| `T` | `<T : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html.https://firebase.google.com/docs/reference/kotlin/com/google/firebase/datastorage/package-summary#(androidx.datastore.preferences.core.Preferences).getOrDefault(androidx.datastore.preferences.core.Preferences.Key,kotlin.Any)(key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key.html<T>, defaultValue: T)` Helper method for getting the value out of a `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html` object if it exists, else falling back to the default value. |

## Extension functions

### getOrDefault

```
fun <T : Any?> Preferences.getOrDefault(key: Preferences.Key<T>, defaultValue: T): T
```

Helper method for getting the value out of a `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.html` object if it exists, else falling back to the default value.

This is primarily useful when working with an instance of `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/MutablePreferences.html`

- like when working within an `https://firebase.google.com/docs/reference/kotlin/com/google/firebase/datastorage/JavaDataStorage#editSync(kotlin.Function1)` callback.

Example:

```kotlin
dataStore.editSync((pref) -> {
 long heartBeatCount = DataStoreKt.getOrDefault(pref, HEART_BEAT_COUNT_TAG, 0L);
 heartBeatCount+=1;
 pref.set(HEART_BEAT_COUNT_TAG, heartBeatCount);

 return null;
});
```

| Parameters |
|---|---|
| `key: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences.Key.html<T>` | The typed key of the entry to get data for. |
| `defaultValue: T` | A value to default to, if the key isn't found. |