You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[shared_preferences] cast exception with get/setStringList: type 'List<Object?>' is not a subtype of type 'List?' #152607
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<Object?>' is not a subtype of type 'List?' in type cast
Expected results
setStringList is not working properly
Actual results
Should be a List<String>, not List<Object>
Code sample
Code sample
finalSharedPreferencesWithCache prefsWithCache =awaitSharedPreferencesWithCache.create(
cacheOptions:constSharedPreferencesWithCacheOptions(
// When an allowlist is included, any keys that aren't included cannot be used.
allowList:<String>{'action'},
),
);
await prefsWithCache!.setStringList('action', <String>['Earth', 'Moon', 'Sun']);
[!] Flutter (Channel stable, 3.22.3, on macOS 14.5 23F79 darwin-arm64, locale en-US) ! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.4.4/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/francesco/fvm/versions/stable. Consider adding /Users/francesco/fvm/versions/stable/bin to the front of your path.[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)[✓] Xcode - develop for iOS and macOS (Xcode 15.4)[✓] Chrome - develop for the web[✓] Android Studio (version 2022.3)[✓] VS Code (version 1.91.1)[✓] Connected device (3 available)[✓] Network resources
is this required? final class SharedPreferencesKeys { static const action = 'action'; }
The docs says just using the string as the key is fine
is not required, this is just me avoiding hard-coding the string 'action' all over the place
you can delete it and replace SharedPreferencesKeys.action with 'action' if you like it better
I have a global variable of:
SharedPreferencesWithCache? prefsWithCache;
In a separate file outside of main, im doing this:
await prefsWithCache!.setStringList('ingredients', ['cherry', 'diamond']);
this is causing this error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<Object?>' is not a subtype of type 'List?' in type cast
#0 SharedPreferencesWithCache.getStringList (package:shared_preferences/src/shared_preferences_async.dart:310:44)
I think it has somethign to do when prefsWithCache is used outside of main? I cant seem to setStringList when its outside of main, it keeps complaining of List<Object?>, no issue when doing setString outside of main
id like for my variables to be stored locally, so when user starts app again the data is stored again. and throughout the app the user can write to shared preferences. im already using providers for state management
@itismejy Please provide a complete reproducible test case that someone can run to see the issue; so far you've only provided fragments that don't compile in isolation, and the description of the error are too vague to be able to investigate (e.g., this comment only shows one line of code and says it causes an exception, but the line of code is a set call and the exception described can only come from get, so there are clearly missing steps there.)
Also, please indicate which platform you are running on, as it may be platform-implementation-specific.
I think this is more a discussion on best practices rather than an issue with flutter,
maybe it's better to close the issue and discuss it on discord
@iapicca Please do not discourage people from reporting exceptions. Whether shared_preferences is the subjectively best tool for the use case, or whether globals are good or bad, is not material to the issue here. If a valid sequence of calls to the shared_preferences API can cause a cast exception, then there's a bug in shared_preferences.
changed the title [-][shared_preferences][/-][+][shared_preferences] cast exception with get/setStringList: `type 'List<Object?>' is not a subtype of type 'List?'`[/+]on Jul 31, 2024
I have a global variable of: SharedPreferencesWithCache? prefsWithCache;
In a separate file outside of main, im doing this: await prefsWithCache!.setStringList('ingredients', ['cherry', 'diamond']);
this is causing this error: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<Object?> > > ' is not a subtype of type 'List?' in type cast #0 SharedPreferencesWithCache.getStringList (package:shared_preferences/src/shared_preferences_async.dart:310:44)
[...]
I think it has somethign to do when prefsWithCache is used outside of main? I cant seem to setStringList when its outside of main, it keeps complaining of List<Object?>, no issue when doing setString outside of main
I think this is more a discussion on best practices rather than an issue with flutter,
maybe it's better to close the issue and discuss it on discord
@iapicca Please do not discourage people from reporting exceptions. Whether shared_preferences is the subjectively best tool for the use case, or whether globals are good or bad, is not material to the issue here. If a valid sequence of calls to the shared_preferences API can cause a cast exception, then there's a bug in shared_preferences.
@stuartmorgan
my bad I thought the reporter (by his/her words [1, 2]) identified the issue in being caused by the use of the package
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
iapicca commentedon Jul 31, 2024
works for me
itismejy commentedon Jul 31, 2024
is this required?
final class SharedPreferencesKeys {
static const action = 'action';
}
The docs says just using the string as the key is fine
iapicca commentedon Jul 31, 2024
is not required, this is just me avoiding hard-coding the string 'action' all over the place
you can delete it and replace
SharedPreferencesKeys.actionwith'action'if you like it betterdoes the code run on your device?
also, you don't need "!" here
itismejy commentedon Jul 31, 2024
I have a global variable of:
SharedPreferencesWithCache? prefsWithCache;
In a separate file outside of main, im doing this:
await prefsWithCache!.setStringList('ingredients', ['cherry', 'diamond']);
this is causing this error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<Object?>' is not a subtype of type 'List?' in type cast
#0 SharedPreferencesWithCache.getStringList (package:shared_preferences/src/shared_preferences_async.dart:310:44)
itismejy commentedon Jul 31, 2024
I think it has somethign to do when prefsWithCache is used outside of main? I cant seem to setStringList when its outside of main, it keeps complaining of List<Object?>, no issue when doing setString outside of main
itismejy commentedon Jul 31, 2024
eg something like this:
void main() async {
await WidgetsFlutterBinding.ensureInitialized();
prefsWithCache = await SharedPreferencesWithCache.create(
cacheOptions: const SharedPreferencesWithCacheOptions(),
);
// await prefsWithCache!.clear();
List? savedIngredients = prefsWithCache!.getStringList('ingredients');
print("ingredients");
// await prefsWithCache!.setStringList('ingredients', ['cherry', 'diamond']);
print(savedIngredients);
runApp(ChangeNotifierProvider(
create: (context) => IngredientsState()..updateState(savedIngredients ?? []), child: const MainApp()));
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@OverRide
Widget build(BuildContext context) {
prefsWithCache!.setStringList('ingredients', ['cherry', 'diamond']);
screenWidth = MediaQuery.of(context).size.width;
screenWidthRatio = screenWidth / prototypeWidth;
screenHeight = MediaQuery.of(context).size.height;
screenHeightRatio = screenHeight / prototypeHeight;
}
}
iapicca commentedon Jul 31, 2024
please don't do that,
if you really really want something similar to a global variable you can use a
singletonsingleton example
but what you might actually need is a state management
I can recommend riverpod.dev
itismejy commentedon Jul 31, 2024
id like for my variables to be stored locally, so when user starts app again the data is stored again. and throughout the app the user can write to shared preferences. im already using providers for state management
iapicca commentedon Jul 31, 2024
I think this is more a discussion on best practices rather than an issue with flutter,
maybe it's better to close the issue and discuss it on discord
darshankawar commentedon Jul 31, 2024
@itismejy
Check the plugin example to see if it helps in your case or not.
Also check if this helps.
stuartmorgan-g commentedon Jul 31, 2024
@itismejy Please provide a complete reproducible test case that someone can run to see the issue; so far you've only provided fragments that don't compile in isolation, and the description of the error are too vague to be able to investigate (e.g., this comment only shows one line of code and says it causes an exception, but the line of code is a
setcall and the exception described can only come fromget, so there are clearly missing steps there.)Also, please indicate which platform you are running on, as it may be platform-implementation-specific.
stuartmorgan-g commentedon Jul 31, 2024
@iapicca Please do not discourage people from reporting exceptions. Whether
shared_preferencesis the subjectively best tool for the use case, or whether globals are good or bad, is not material to the issue here. If a valid sequence of calls to theshared_preferencesAPI can cause a cast exception, then there's a bug inshared_preferences.[-][shared_preferences][/-][+][shared_preferences] cast exception with get/setStringList: `type 'List<Object?>' is not a subtype of type 'List?'`[/+]4 remaining items
iapicca commentedon Jul 31, 2024
@stuartmorgan
my bad I thought the reporter (by his/her words [1, 2]) identified the issue in being caused by the use of the package
itismejy commentedon Aug 1, 2024
Thanks guys for your work!
[shared_preferences] fix stringlist bug (flutter#7268)
github-actions commentedon Aug 16, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.[shared_preferences] fix stringlist bug (flutter#7268)
[shared_preferences] fix stringlist bug (flutter#7268)