skip to Main Content

I want to store two data(user,password) from two textfields using shared preference package in flutter

I created a function

saveData(String Name, String pass) async {
  final prefs = await SharedPreferences.getInstance();
  prefs.setString(Name, pass);
}


Now I want to get the data from two textFields

class Login extends StatefulWidget {
  const Login({super.key});

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('LOGIN'),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(25.0),
            child: Container(
              height: 300,
              width: 500,
              color: Colors.black12,
              child: Padding(
                padding: const EdgeInsets.all(30.0),
                child: Column(
                  children: [TextField(), TextField()],
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

4

Answers


  1. try initialize 2x controller

    final nameController = TextEditingController();

    final psswdController = TextEditingController();

    in nex step add this controller to Text area like:

    Column(
     children: [
       TextField(
        controller: nameController,
       ),
       TextField(
        controller: psswdController,
       ),
      ],
    ),
    

    And when need get data from this areas call:

    nameController.text

    psswdController.text

    like simple:

     saveData(nameController.text,psswdController.text);
    
    Login or Signup to reply.
  2. First of all, to store data in shared preference, you have to store it like a key value pair. Your saveData method should look like this,

    saveData(String key, String value) async {
        final prefs = await SharedPreferences.getInstance();
        prefs.setString(key, value);
    }
    

    To get the value from textField you have to use textEditingController. To get more information you can follow this link
    How to get the TextField value in flutter

    Login or Signup to reply.
  3. I’m Trying to Solve your problem :

    Create a button to save the data and two TextFields for users to enter their usernames and passwords:

    TextEditingController _usernameController = TextEditingController();
    TextEditingController _passwordController = TextEditingController();
    
    ...
    
    TextField(
      controller: _usernameController,
      decoration: InputDecoration(
        hintText: 'Username',
      ),
    ),
    TextField(
      controller: _passwordController,
      decoration: InputDecoration(
        hintText: 'Password',
      ),
      obscureText: true,
    ),
    ElevatedButton(
      onPressed: () async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        await prefs.setString('username', _usernameController.text);
        await prefs.setString('password', _passwordController.text);
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Data saved successfully')),
        );
      },
      child: Text('Save'),
    ),
    

    You can use the getString() method to get the saved data Like This :

    SharedPreferences prefs = await SharedPreferences.getInstance();
    String username = prefs.getString('username') ?? '';
    String password = prefs.getString('password') ?? '';
    
    Login or Signup to reply.
  4. You can’t saved name and password like that. prefs.setString have parameter key and value. Key for accessing the value of sharedprefs.

    ex:

    prefs.setString("username", "vidhu");
    prefs.getString("username"); //"vidhu"
    

    so, from your code above you need to adjust

    saveData(String Name, String pass) async {
      final prefs = await SharedPreferences.getInstance();
      prefs.setString("name", Name);
      prefs.setString("pass", pass);
    }
    
    

    and add TextFieldController for each textfield

    Column(
     children: [
       TextField(
        controller: nameController,
       ),
       TextField(
        controller: passwordController,
       ),
      ],
    )
    

    then you can call saveData() from button

    ElevatedButton(onPressed:saveData(nameController.text, passwordController.text), child: Text('Login')),
    

    so when you access name and pass

    print(prefs.getString("name")); //it will print your "Name"
    print(prefs.getString("pass")); //it will print your "pass"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search