Skip to content

Instantly share code, notes, and snippets.

@ttlg
Last active July 21, 2021 16:40
Show Gist options
  • Save ttlg/53d74cbdcc2ef4f3610ee561c9dd64c0 to your computer and use it in GitHub Desktop.
Save ttlg/53d74cbdcc2ef4f3610ee561c9dd64c0 to your computer and use it in GitHub Desktop.
SharedPreferencesClient
import 'dart:convert';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
final sharedPreferencesClient = Provider((ref) => SharedPreferencesClient());
class SharedPreferencesClient {
Future<List<Map<String, dynamic>>> getJsonList(String key) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return List<Map<String, dynamic>>.from(
jsonDecode(prefs.getString(key) ?? '[]'));
}
Future<void> saveJson(String key, List<Map<String, dynamic>> json) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(key, jsonEncode(json));
}
}
@ttlg
Copy link
Author

ttlg commented Sep 5, 2020

Pragmatic architecture using Riverpod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment