Hi! Can you write me a Flutter class that will provide API to work with Shared…
- User
Hi! Can you write me a Flutter class that will provide API to work with Shared Preferences package and also it should use injectable
- Assistant
Sure! Here's an example of a Flutter class that uses the Shared Preferences package and implements dependency injection using the injectable package:
import 'package:shared_preferences/shared_preferences.dart'; import 'package:injectable/injectable.dart'; @Injectable() class SharedPreferencesService { final SharedPreferences _sharedPreferences; SharedPreferencesService(this._sharedPreferences); // Get value from Shared Preferences Future<String?> getValue(String key) async { return _sharedPreferences.getString(key); } // Set value to Shared Preferences Future<void> setValue(String key, String value) async { await _sharedPreferences.setString(key, value); } // Remove value from Shared Preferences Future<void> removeValue(String key) async { await _sharedPreferences.remove(key); } }