1

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?

davidism
  • 121,510
  • 29
  • 395
  • 339
  • check https://pub.dev/packages/flutter_secure_storage – griffins Sep 14 '22 at 17:12
  • Does this answer your question? [Store model data into Flutter Secure Storage](https://stackoverflow.com/questions/66253148/store-model-data-into-flutter-secure-storage) – griffins Sep 14 '22 at 17:13
  • But how do i extract the cookie from response? – Ananta Dev Datta Sep 14 '22 at 17:14
  • 1) The "standard" way to access cookies is to embed some Javascript in your HTML. Perhaps this is an option for you. 2) For Flutter, you have (at least) two challenges: a) programmatically reading the cookie, and b) persisting it to local storage. SUGGESTION: start here: https://api.flutter.dev/flutter/dart-io/Cookie-class.html or here: https://rrtutors.com/tutorials/How-do-i-set-Cookie-to-Http-Request – paulsm4 Sep 14 '22 at 17:14
  • It"s making me more confused, i just want the part to extract cookie and how to use it. I – Ananta Dev Datta Sep 14 '22 at 17:37

1 Answers1

2

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:

paulsm4
  • 114,292
  • 17
  • 138
  • 190