Skip to content

[shared_perferences] getStringList in new APIs doesn't work correctly on Windows/Linux #153106

@paulober

Description

@paulober

Steps to reproduce

Use getStringList with SharedPreferencesAsync on Windowx 64.

A workaround would be to use the legacy API again :)

final prefs = await SharedPreferences.getInstance();
summaryList = prefs.getStringList(settingsKey) ?? [];

Expected results

It works.

Actual results

Type Error:

type 'List<dynamic>' is not a subtype of type 'List<String>?' in type cast

Code sample

Code sample
static Future<List<Summary>> loadSummaries() async {
    final prefs = SharedPreferencesAsync();
    try {
      final summaryList = await prefs.getStringList('summaryList') ?? [];

      return summaryList.map((jsonString) {
        final jsonMap = jsonDecode(jsonString);
        return Summary.fromJson(jsonMap);
      }).toList();
    } catch (e) {
      if (e is TypeError) {
        debugPrint("[Summary] SummaryList is corrupted: $e");
      }
      return [];
    }
  }

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.24.0, on Microsoft Windows [Version 10.0.22631.3958], locale de-DE)
[✓] Windows Version (Installed version of Windows is version 10 or higher)

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.platform-linuxBuilding on or for Linux specificallyplatform-windowsBuilding on or for Windows specificallyteam-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