[FIXED] How do I save flask login session in flutter?

Issue

So I have a flutter app. And also I have a backend api with flask. The way the api works is, if you log into the api, the api will let you use a database and you can create data there. We can easily do this in browser. In browser or Postman, it automatically saves the login session. But in flutter they don’t save the login session automatically. What is the best way to save login session just like a browser in flutter?

Solution

You can jsonDecode(response.body) and then store the information therein in SharedPreferences (package that abstracts access to local device storage). If you have need for the cookie, you can find it in the jsonDecode(response.headers). Store this information in an object then save to sharedpreferences. Once that is done, set up your app to always fetch the info first at start up so that it can be used in the application through whatever state management you use

To POST the data, install and use this package called http. There are instructions on how to use it but here is a short snippet.

import 'dart:developer';
import 'package:http/http.dart' as http;

Future postExample() async{
  final url = Uri.parse('https://your-url.com');
  final response = await http.post(url, headers{}, body{});//add required data in headers and body although for logging in the header would typically be empty
  log(response.body); //This is just to see the output in the terminal

}

Answered By – O'neya

Answer Checked By – Terry (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published