How do I save session cookie from response in flutter?

Viewed 42

Let's say I log into an api with my flutter app. In an website, they automatically store the login cookie and then can use it. But in flutter app, how do I store the cookies and session? And how do I pass that into post to let the api know I have a valid login session?

1 Answers

Q: It's making me more confused, I just want the part to extract cookie and how to use it.

There are a number of complexities, depending on exactly what you ultimately want to do.

But let's assume:

  • Your Flutter app makes an HTTP request (GET, PUT, etc.)

  • The server (e.g. your Flask app) returns cookies in the HTTP response (in the HTTP response header).

  • Let's further assume your HTTP code looks something like this:

    Future<http.Response> fetchAlbum() {
      return http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
    }
    
  • In that case, you should be able to reference the cookies property of the Response object returned from the server.

SUGGESTION: See also these links:

Related