Created
July 6, 2019 02:47
-
-
Save hgodinez89/c566ff9dc82c45f767d5ec54819ca162 to your computer and use it in GitHub Desktop.
Gist para almacenar preferencias del usuario en storage local del movil
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 'package:shared_preferences/shared_preferences.dart'; | |
/* | |
Recordar instalar el paquete de: | |
shared_preferences: | |
Inicializar en el main | |
final prefs = new PreferenciasUsuario(); | |
await prefs.initPrefs(); | |
Recuerden que el main() debe de ser async {... | |
*/ | |
class PreferenciasUsuario { | |
static final PreferenciasUsuario _instancia = new PreferenciasUsuario._internal(); | |
factory PreferenciasUsuario() { | |
return _instancia; | |
} | |
PreferenciasUsuario._internal(); | |
SharedPreferences _prefs; | |
initPrefs() async { | |
this._prefs = await SharedPreferences.getInstance(); | |
} | |
// GET y SET del nombre | |
get nombre { | |
return _prefs.getString('nombre') ?? ''; | |
} | |
set nombre( String value ) { | |
_prefs.setString('nombre', value); | |
} | |
// GET y SET de la última página | |
get ultimaPagina { | |
return _prefs.getString('ultimaPagina') ?? 'login'; | |
} | |
set ultimaPagina( String value ) { | |
_prefs.setString('ultimaPagina', value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment