Skip to content

SharedPreferencesOptions not available in SharedPreferences package #162381

@jacob1710

Description

@jacob1710

Steps to reproduce

Following the Migrating from SharedPreferences to SharedPreferencesAsync/WithCache code on the pub.dev page (https://pub.dev/packages/shared_preferences#migration-and-prefixes). The class SharedPreferencesOptions is unable to be found in the shared_preferences package. It needs to be imported from 'package:shared_preferences_platform_interface/types.dart' which however gives a warning: The imported package 'shared_preferences_platform_interface' isn't a dependency of the importing package. (Documentation).

Expected results

The SharedPreferencesOptions class can be imported, and the migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary function can be ran.

Actual results

If the class is not imported, a Error: 'SharedPreferencesOptions' isn't a type. error prevents the app from building. If the class is imported, it runs fine however importing from shared_preferences_platform_interface shows a warning

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences/util/legacy_to_async_migration_util.dart';
//SharedPreferencesOptions Import
import 'package:shared_preferences_platform_interface/types.dart';

void main() async {
  SharedPreferencesOptions sharedPreferencesOptions =
      SharedPreferencesOptions();
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
    legacySharedPreferencesInstance: prefs,
    sharedPreferencesAsyncOptions: sharedPreferencesOptions,
    migrationCompletedKey: 'migrationCompleted',
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
        ),
        home: Placeholder());
  }
}






Pubspec: 


dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^2.4.0

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
The imported package 'shared_preferences_platform_interface' isn't a dependency of the importing package. (Documentation)  Try adding a dependency for 'shared_preferences_platform_interface' in the 'pubspec. yaml' file.

or 

lib/main.dart:34:3: Error: 'SharedPreferencesOptions' isn't a type.
  SharedPreferencesOptions sharedPreferencesOptions =
  ^^^^^^^^^^^^^^^^^^^^^^^^
lib/main.dart:35:7: Error: Method not found: 'SharedPreferencesOptions'.
      SharedPreferencesOptions();
      ^^^^^^^^^^^^^^^^^^^^^^^^
Target kernel_snapshot_program failed: Exception


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDevDebug'.
> Process 'command '/Users/jacobscase/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 11s
Error: Gradle task assembleDevDebug failed with exit code 1
[Paste your logs here]

Flutter Doctor output

Doctor output

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listp: shared_preferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions