-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Closed
flutter/packages
#7355Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listp: shared_preferencesPlugin to read and write Shared PreferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-ecosystemOwned by Ecosystem teamOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem teamTriaged by Ecosystem team
Description
Steps to reproduce
See the code sample which uses the SharedPreferences to get a nullable list async within a try-catch.
And run the app on Android with API level 34.
My uneducated guess would be that this is some kind of compiler optimization as the list is async returned in the inner scope of two scopes down and then replaces the value of a empty list.
Expected results
That the list remains mutable as SharedPreferences promisses you to returns a mutable list.
Actual results
E/flutter (23889): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Unsupported operation: Cannot add to a fixed-length list
E/flutter (23889): #0 FixedLengthListMixin.add (dart:_internal/list.dart:21:5)
E/flutter (23889): #1 _CastListBase.add (dart:_internal/cast.dart:110:13)
E/flutter (23889): #2 Summary.saveSummary (package:myapp/models/summary.dart:105:17)
E/flutter (23889): <asynchronous suspension>
E/flutter (23889):
W/WindowOnBackDispatcher(23889): sendCancelIfRunning: isInProgress=falsecallback=io.flutter.embedding.android.FlutterActivity$1@5be4377Code sample
Code sample
static Future<void> saveSummary(Summary summary) async {
List<String> summaryList = [];
try {
if (Platform.isWindows) {
final prefs = await SharedPreferences.getInstance();
summaryList = prefs.getStringList(settingsKey) ?? [];
} else {
final prefs = SharedPreferencesAsync();
summaryList = await prefs.getStringList(settingsKey) ?? [];
}
} catch (e) {
if (e is TypeError) {
debugPrint("[Summary] SummaryList is corrupted. Resetting it.");
}
}
summaryList.add(jsonEncode(summary.toJson())); // line 105
if (Platform.isWindows) {
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList(settingsKey, summaryList);
} else {
final prefs = SharedPreferencesAsync();
await prefs.setStringList(settingsKey, summaryList);
}
}can be fixed by using List.from
else {
final prefs = SharedPreferencesAsync();
summaryList =
List<String>.from(await prefs.getStringList(settingsKey) ?? []);
}Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
E/flutter (23889): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Unsupported operation: Cannot add to a fixed-length list
E/flutter (23889): #0 FixedLengthListMixin.add (dart:_internal/list.dart:21:5)
E/flutter (23889): #1 _CastListBase.add (dart:_internal/cast.dart:110:13)
E/flutter (23889): #2 Summary.saveSummary (package:myapp/models/summary.dart:105:17)
E/flutter (23889): <asynchronous suspension>
E/flutter (23889):
W/WindowOnBackDispatcher(23889): sendCancelIfRunning: isInProgress=falsecallback=io.flutter.embedding.android.FlutterActivity$1@5be4377Flutter Doctor output
Doctor output
[✓] Flutter (Channel stable, 3.24.0, on macOS 15.0 24A5309e darwin-arm64, locale
en-US)
• Flutter version 3.24.0 on channel stable at
/Users/paulober/flutterdev/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 80c2e84975 (9 days ago), 2024-07-30 23:06:49 +0700
• Engine revision b8800d88be
• Dart version 3.5.0
• DevTools version 2.37.2
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
• Android SDK at /Users/paulober/Library/Android/sdk
• Platform android-35, build-tools 35.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.Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listp: shared_preferencesPlugin to read and write Shared PreferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-ecosystemOwned by Ecosystem teamOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem teamTriaged by Ecosystem team