Repository: flutter
Issue: [shared_preferences] Access to FlutterSharedPreferences data store instance
Labels: platform-android,p: shared_preferences,package,c: proposal,P3,team-android,triaged-android

### What package does this bug report belong to?

shared_preferences

### What target platforms are you seeing this bug on?

Android

### Have you already upgraded your packages?

Yes

### Dependency versions

_No response_

### Steps to reproduce

Create a `DataStore` instance pointing to the same file as the SharedPreferences plugin:

```kotlin
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "FlutterSharedPreferences")
```

### Expected results

Accessing data from `FlutterSharedPreferences` in native code would be beneficial. One use case is the native implementation of App Widgets, where a common approach is to communicate via DataStore/SharedPreferences.

Changing the visibility of [`Context.sharedPreferencesDataStore`](https://github.com/flutter/packages/blob/bcb09dbc121e5f886a37cf2808c0d95112276fcd/packages/shared_preferences/shared_preferences_android/android/src/main/kotlin/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.kt#L34) to public could solve the problem, but I understand this might not be the ideal solution.

Are there any other options available?


### Actual results

According to the [documentation](https://developer.android.com/topic/libraries/architecture/datastore#correct_usage), we should not create multiple instances for the same file, as this can lead to the following error:
```
There are multiple DataStores active for the same file: /data/user/0/com.example/files/datastore/FlutterSharedPreferences.preferences_pb. You should either maintain your DataStore as a singleton or confirm that there is no two DataStore's active on the same file (by confirming that the scope is cancelled).
```

Currently, there is no way to access a singleton instance of the DataStore.

### Code sample

<details open><summary>Code sample</summary>

The native app widget example is provided to illustrate a potential use case, which, of course, leads to an error due to the use of the same file instance.

```kotlin
package app.getwatermelon.mobile

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import androidx.glance.GlanceId
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.provideContent
import androidx.glance.text.Text
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "FlutterSharedPreferences")

class ExampleAppWidget : GlanceAppWidget() {

    val value = longPreferencesKey("value_key")

    override suspend fun provideGlance(context: Context, id: GlanceId) {
        val preferences = runBlocking { context.dataStore.data.first() }
        val value = preferences[value] ?: 0
        provideContent {
            WidgetContent()
        }
    }

    @Composable
    private fun WidgetContent() {
        Text("Hello world")
    }
}

```

</details>


### Screenshots or Videos

_No response_

### Logs

_No response_

### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
flutter doctor -v
[✓] Flutter (Channel stable, 3.24.1, on macOS 14.5 23F79 darwin-arm64, locale en-PL)
    • Flutter version 3.24.1 on channel stable at /Users/lawinski/Develop/sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5874a72aa4 (7 days ago), 2024-08-20 16:46:00 -0500
    • Engine revision c9b9d5780d
    • Dart version 3.5.1
    • DevTools version 2.37.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/lawinski/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)

[✓] Android Studio (version 2024.1)
    • Android Studio at /Users/lawinski/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)

[✓] IntelliJ IDEA Community Edition (version 2023.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.92.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.94.0
```

</details>