Last active
June 7, 2020 20:00
-
-
Save silasrm/ba241331586681fb8eaf6ae27b60ba8f to your computer and use it in GitHub Desktop.
Manager storage data on json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies: | |
path_provider: ^1.6.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'dart:convert'; | |
import 'package:path_provider/path_provider.dart'; | |
/// Storage storage = new Storage(); | |
class Storage { | |
Map<String, dynamic> _lastRemoved; | |
int _lastRemovedIdx; | |
Future<File> _getFile() async { | |
final directory = await getApplicationDocumentsDirectory(); | |
return File('${directory.path}/data.json'); | |
} | |
Future<File> saveData(List list) async { | |
String data = json.encode(list); | |
final file = await _getFile(); | |
return file.writeAsString(data); | |
} | |
/// @usage on initState(); | |
/// storage.readData().then((data) { | |
/// setState(() { | |
/// _history = json.decode(data); | |
/// _history.sort((a, b) { | |
/// DateTime aDate = DateTime.parse(a['date']); | |
/// DateTime bDate = DateTime.parse(b['date']); | |
/// | |
/// if (aDate.millisecondsSinceEpoch == bDate.millisecondsSinceEpoch) { | |
/// return 0; | |
/// } | |
/// | |
/// return aDate.millisecondsSinceEpoch > bDate.millisecondsSinceEpoch ? -1 : 1; | |
/// }); | |
/// }); | |
/// }); | |
/// | |
/// @usage on RefreshIndicator::onRefresh; | |
/// storage.readData().then((data) { | |
/// setState(() { | |
/// _history = json.decode(data); | |
/// _history.sort((a, b) { | |
/// DateTime aDate = DateTime.parse(a['date']); | |
/// DateTime bDate = DateTime.parse(b['date']); | |
/// | |
/// if (aDate.millisecondsSinceEpoch == bDate.millisecondsSinceEpoch) { | |
/// return 0; | |
/// } | |
/// | |
/// return aDate.millisecondsSinceEpoch > bDate.millisecondsSinceEpoch ? -1 : 1; | |
/// }); | |
/// | |
/// storage.saveData(_history); | |
/// }); | |
/// }); | |
Future<String> readData() async { | |
try { | |
final file = await _getFile(); | |
return file.readAsString(); | |
} catch(e) { | |
return null; | |
} | |
} | |
/// @usage | |
/// Map<String, dynamic> entry = Map(); | |
/// entry['name'] = name; | |
/// entry['...'] = ...; | |
/// | |
/// storage.add(entry).then((value) { | |
/// setState(() { | |
/// _resultSaved = true; | |
/// | |
/// final snack = SnackBar( | |
/// content: Text('Dados salvo com sucesso!'), | |
/// duration: Duration(seconds: 3), | |
/// ); | |
/// | |
/// _scaffoldKey.currentState.removeCurrentSnackBar(); | |
/// _scaffoldKey.currentState.showSnackBar(snack); | |
/// }); | |
/// }); | |
Future<File> add(Map entry) { | |
return this.readData().then((data) { | |
List history = []; | |
history = json.decode(data); | |
history.add(entry); | |
return saveData(history); | |
}); | |
} | |
/// @usage | |
/// storage.removeAt(index).then((file) { | |
/// file.readAsString().then((data) { | |
/// setState(() { | |
/// _history = json.decode(data); | |
/// | |
/// final snack = SnackBar( | |
/// content: Text('Dados removidos com sucesso!'), | |
/// duration: Duration(seconds: 3), | |
/// action: SnackBarAction( | |
/// label: 'Desfazer', | |
/// onPressed: () { | |
/// storage.insertLastRemoved().then((file) { | |
/// file.readAsString().then((data) { | |
/// setState(() { | |
/// _history = json.decode(data); | |
/// }); | |
/// }); | |
/// }); | |
/// }, | |
/// ), | |
/// ); | |
/// | |
/// _scaffoldKey.currentState.removeCurrentSnackBar(); | |
/// _scaffoldKey.currentState.showSnackBar(snack); | |
/// }); | |
/// }); | |
/// }); | |
Future<File> removeAt(int index) { | |
return this.readData().then((data) { | |
List history = []; | |
history = json.decode(data); | |
_lastRemoved = Map.from(history[index]); | |
_lastRemovedIdx = index; | |
history.removeAt(index); | |
return saveData(history); | |
}); | |
} | |
Future<File> insertLastRemoved() { | |
return this.readData().then((data) { | |
List history = []; | |
history = json.decode(data); | |
history.insert(_lastRemovedIdx, _lastRemoved); | |
return saveData(history); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment