flutter_data_shp_provider 0.0.1
flutter_data_shp_provider: ^0.0.1 copied to clipboard
A singleton wrapper around SharedPreferences for managing authentication and session data with built-in error handling and debug logging.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_data_shp_provider/flutter_data_shp_provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize SharedPreferences before running the app
await DataShpProvider().initPrefs();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'DataShpProvider Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const ExampleHomePage(),
);
}
}
class ExampleHomePage extends StatefulWidget {
const ExampleHomePage({super.key});
@override
State<ExampleHomePage> createState() => _ExampleHomePageState();
}
class _ExampleHomePageState extends State<ExampleHomePage> {
final _provider = DataShpProvider();
final _keyController = TextEditingController();
final _valueController = TextEditingController();
String _readValue = '';
String _statusMessage = '';
@override
void initState() {
super.initState();
_checkInitialization();
}
void _checkInitialization() {
if (_provider.isInitialized) {
setState(() {
_statusMessage = 'Provider initialized successfully';
});
} else {
setState(() {
_statusMessage = 'Provider not initialized';
});
}
}
Future<void> _saveGenericValue() async {
if (_keyController.text.isEmpty || _valueController.text.isEmpty) {
_showMessage('Please enter both key and value');
return;
}
final success = await _provider.saveValue(
_keyController.text,
_valueController.text,
);
_showMessage(success
? 'Value saved successfully'
: 'Failed to save value');
}
void _readGenericValue() {
if (_keyController.text.isEmpty) {
_showMessage('Please enter a key');
return;
}
final value = _provider.getValue(
_keyController.text,
defaultValue: 'Not found',
);
setState(() {
_readValue = value;
});
}
Future<void> _saveAuthData() async {
await _provider.setKeyLogin('user@example.com');
await _provider.setPasswordLogin('demo-password');
await _provider.setToken('abc123-demo-token');
await _provider.setIdSesion('session-12345');
await _provider.setValidityInSeconds('3600');
_showMessage('Auth data saved successfully');
}
void _readAuthData() {
final authData = '''
Login: ${_provider.keyLogin}
Token: ${_provider.token}
Session ID: ${_provider.idSesion}
Validity: ${_provider.validityInSeconds}s
''';
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Stored Auth Data'),
content: Text(authData),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'),
),
],
),
);
}
Future<void> _clearAllPreferences() async {
final confirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Confirm'),
content: const Text('Clear all preferences?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Clear'),
),
],
),
);
if (confirmed == true) {
final success = await _provider.limpiarPreferencias();
_showMessage(success
? 'All preferences cleared'
: 'Failed to clear preferences');
setState(() {
_readValue = '';
_keyController.clear();
_valueController.clear();
});
}
}
void _showMessage(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
}
@override
void dispose() {
_keyController.dispose();
_valueController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('DataShpProvider Example'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Initialization status
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
const Text(
'Initialization Status',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(_statusMessage),
],
),
),
),
const SizedBox(height: 24),
// Generic getValue/saveValue
const Text(
'Generic Storage',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
TextField(
controller: _keyController,
decoration: const InputDecoration(
labelText: 'Key',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 8),
TextField(
controller: _valueController,
decoration: const InputDecoration(
labelText: 'Value',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: _saveGenericValue,
child: const Text('Save'),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton(
onPressed: _readGenericValue,
child: const Text('Read'),
),
),
],
),
if (_readValue.isNotEmpty) ...[
const SizedBox(height: 8),
Card(
color: Colors.green.shade50,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Read value: $_readValue'),
),
),
],
const SizedBox(height: 24),
// Authentication data
const Text(
'Authentication Data',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _saveAuthData,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
child: const Text('Save Demo Auth Data'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _readAuthData,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
child: const Text('Read Auth Data'),
),
const SizedBox(height: 24),
// Clear all
const Text(
'Danger Zone',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _clearAllPreferences,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
child: const Text('Clear All Preferences'),
),
],
),
),
);
}
}