Paul Coshott
Paul Coshott

Reputation: 1031

How to save info such as a user name to the device?

I'd like to provide a "Remember Me" for my flutter app's login screen. Can someone point me in the right direction of how I can read and write some info to the device such as the user name.

Upvotes: 1

Views: 463

Answers (2)

Sanjeev Sangral
Sanjeev Sangral

Reputation: 1417

With the help of SharedPreferences you can store the info locally use the dependencies in pubspec.yaml file

dependencies:
  shared_preferences: ^0.5.7+3

and import the package :

import 'package:shared_preferences/shared_preferences.dart';



String userName;
String userId;
String salary;



_saveValues(bool val, int screen) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString("name", responseJson.name);
    prefs.setString("id", "userId");
    prefs.setString("salary", "1200000");

}

getSharedPreferencesValue() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    userName  = prefs.getString("name")?? "";
    userId  = prefs.getString("password")?? "";
    salary  = prefs.getString("salary")?? "";
}

Hope this will help you!!

Upvotes: 2

jared
jared

Reputation: 483

Have you looked into shared preferences? https://pub.dev/packages/shared_preferences

Upvotes: 1

Related Questions