# Flutter save credentials

**URL:** <https://community.auth0.com/t/flutter-save-credentials/119117>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [October 22, 2023, 10:04pm UTC](https://community.auth0.com/t/flutter-save-credentials/119117 "2023-10-22T22:04:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakkipally](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jakkipally/32/50200_2.png) [@jakkipally](https://community.auth0.com/u/jakkipally)\
**Post date:** [October 22, 2023, 10:04pm UTC](https://community.auth0.com/t/flutter-save-credentials/119117/1 "2023-10-22T22:04:40Z")

</div>

Hi guys,

I’m creating a flutter app, do anyone know how to save the credentials? The quickstart does not show how to do this (unlike the iOS Swift Quickstart), and I could not find anything on the docs.

This is my app so far:

```auto
Credentials? _credentials;
...
await auth0.webAuthentication(scheme: 'demo').login();
setState(() {
   _credentials = credentials;
});

```

I think all I have to do is store the data and validate the credentials, but I’m not sure how to do that. Does anyone have any idea how to?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 24, 2023, 10:54pm UTC](https://community.auth0.com/t/flutter-save-credentials/119117/3 "2023-10-24T22:54:52Z")

</div>

Hey @jakkipally !

Good question - After logging in you get a `Credentials` instance which contains an access token and an ID token (which in turn contains the user profile data, that is exposed through the `user` property as well). That is, the SDK should handle storing the credentials for you.

---

<div class="post-metadata">

**Author:** ![jakkipally](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jakkipally/32/50200_2.png) [@jakkipally](https://community.auth0.com/u/jakkipally)\
**Post date:** [October 27, 2023, 11:34pm UTC](https://community.auth0.com/t/flutter-save-credentials/119117/4 "2023-10-27T23:34:11Z")

</div>

Thanks @ty.frith. However, I don’t think that the credentials are being saved. I’ll double check again, but last time, I had to after leaving my app, and when the popup appears, I don’t have to sign in and it redirects to logged in page.

---

<div class="post-metadata">

**Author:** ![jakkipally](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jakkipally/32/50200_2.png) [@jakkipally](https://community.auth0.com/u/jakkipally)\
**Post date:** [October 28, 2023, 3:54pm UTC](https://community.auth0.com/t/flutter-save-credentials/119117/6 "2023-10-28T15:54:44Z")

</div>

@ty.frith Just for some clarification, if I use this code to show login or show the logged in page:

```auto
print(_credentials);
if (_credentials == null) {
// logged out
} else {
//logged in
}

```

I’m not sure if this is the problem because I’m doing something wrong, but I get this log:

 ![Screenshot 2023-10-28 at 11.48.38 AM](https://global.discourse-cdn.com/auth0/original/3X/3/b/3b712e17d8c49fa49920185db00688983626e19b.png)

`Instance of 'Credentials'` is when I log in and it successfully logs my credentials

Then when I exit the tab and re-enter it, I get `null`, showing that when I initialize:

```auto
Credentials? _credentials;

```

It does not store the credentials when I do:

```auto
setState(() {
   _credentials = credentials;
});

```

​  
What I think is that I’m probably saving the credentials but not using the credentialsManager `hasValidCredentials`. I’ll look into the credential manager api and try and figure it out.

---

<div class="post-metadata">

**Author:** ![jakkipally](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jakkipally/32/50200_2.png) [@jakkipally](https://community.auth0.com/u/jakkipally)\
**Post date:** [November 6, 2023, 1:14am UTC](https://community.auth0.com/t/flutter-save-credentials/119117/7 "2023-11-06T01:14:56Z")

</div>

Hey guys, I figured it out myself!

```auto
  Credentials? _credentials;
  ...
  bool? isLoggedIn;
  

  @override
  void initState() {
    super.initState();
    ...
    _getData();
  }

  void _getData() async {
    isLoggedIn = await auth0.credentialsManager.hasValidCredentials();
    _credentials = await auth0.credentialsManager.credentials();
    setState(() {});
  }

```

And then in the widget build:

```auto
if (isLoggedIn == null) {
   if (_credentials == null) {
      // needs to login
   } else {
      // loading credentials
   }
} else {
   if (_credentials == null) {
      // logged in with credentials
   } else {
      // loading
   }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 14, 2023, 11:08pm UTC](https://community.auth0.com/t/flutter-save-credentials/119117/8 "2023-12-14T23:08:52Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
