You can use shared preferences to save the user ID, because you are using an SQL Server DB perhaps you will provide also a token and need to store the token too.
On the first login of the user save the data after the login is done successfully.
import 'package:shared_preferences/shared_preferences.dart';
var _prefs = SharedPreferences.getInstance();
/// ----------------------------------------------------------
/// Method that saves the token (if you need the token) and userID in Shared Preferences
/// ----------------------------------------------------------
Future<bool> _setMobileToken(String token , String username, String userID) async {
final SharedPreferences prefs = await _prefs;
prefs.setString("username", username);
prefs.setString("userID", userID);
return prefs.setString(_storageKeyMobileToken, token);
}
After that when you close the app and open in again you just need to read your data from Shared Preferences.
// Async function that calls getSharedPreferences
awaitSharedPref() async {
await getSharedPreferences();
}
// Get the username and token from SharedPreferences and check if the token is valid
awaitToken() async {
await functions.getMobileToken();
if (token != "" && token != null && username!= "" && username!= null ) {
// If you use any token try to make a validation if was expired or not
//_presenter.validateToken(token, username.toString());
// If not redirect the user directly on the home page
Navigator.of(_ctx).pushReplacementNamed("/home");
} else {
Navigator.of(context).pushNamed("/login");
}
}
If you want you can also add a "Remember me" checkbox, so even if he log out from the app the next time he will open it, can click directly the "Login" button with username and pass autocomplete.
final rememberMe = CheckboxListTile(
value: checkValue,
onChanged: _onChanged,
title: new Text("Remember me", style: new TextStyle(
color: Colors.blueGrey[600], fontStyle: FontStyle.italic),),
controlAffinity: ListTileControlAffinity.leading,
);
_onChanged(bool value) async {
sharedPreferences = await SharedPreferences.getInstance();
setState(() {
checkValue = value;
sharedPreferences.setBool("check", checkValue);
sharedPreferences.setString("username", username.text);
sharedPreferences.setString("password", password.text);
getCredential();
});
}