skip to Main Content

I want to create a cookie for storage for a post request but i am facing challlenges on what to understand

I tried creating a cookie class and a handle cookie method but the creation doesnt seem to work and it stops my login from working

2

Answers


  1. For saving any data to local storage of the device you can use several solutions in flutter.

    Especially for your problem I recommend flutter_secure_storage. It is a good option for storing simple and sensitive data.

    Login or Signup to reply.
  2. To access the cookie you need the cookie_jar and dio_cookie_manager package and to use the cookie you need to store the cookie in fileStorage and to store, you need the documentsDirectory so you need the path provider package.

    import 'dart:io';
    import 'package:dio/dio.dart';
    import 'package:cookie_jar/cookie_jar.dart';
    import 'package:dio_cookie_manager/dio_cookie_manager.dart';
    import 'package:path_provider/path_provider.dart';
    
    class WebService {    
      WebService();
    
    
      final dio = Dio();
      var cookieJar;
    
      Future<bool> setCookieJar() async {
        if (this.cookieJar == null) {
          try {
            Directory appDocDir = await getApplicationDocumentsDirectory();
            String appDocPath = appDocDir.path;
            var cookieJar = PersistCookieJar(
                ignoreExpires: true,
                storage: FileStorage(appDocPath + "/.cookies/"));
    
            print('it is cookie    setCookieJar  ' + cookieJar.toString());
            dio.interceptors.add(CookieManager(cookieJar));
    
            return true;
          } catch (e) {
            Exception("exception on cookie");
          }
        }
        return true;
      }
    }
    

    after adding these packages you can use the WebService class to inject the cookie to network package like dio.

    and you can write your rest methods (get, post,….) inside the WebService as follows.

    import 'dart:io';
    import 'package:dio/dio.dart';
    import 'package:cookie_jar/cookie_jar.dart';
    import 'package:dio_cookie_manager/dio_cookie_manager.dart';
    import 'package:path_provider/path_provider.dart';
    
    class WebService {    
      WebService();
      final dio = Dio();
      var cookieJar;
    
      Future<bool> setCookieJar() async {
        if (this.cookieJar == null) {
          try {
            Directory appDocDir = await getApplicationDocumentsDirectory();
            String appDocPath = appDocDir.path;
            var cookieJar = PersistCookieJar(
                ignoreExpires: true,
                storage: FileStorage(appDocPath + "/.cookies/"));
    
            print('it is cookie    setCookieJar  ' + cookieJar.toString());
            dio.interceptors.add(CookieManager(cookieJar));
    
            return true;
          } catch (e) {
            Exception("exception on cookie");
          }
        }
        return true;
      }
    
        Future<ServerResponse> getFunction() async {
        Response response;
        try {
          await setCookieJar();
          response = await dio.get(url);
        } catch (errorMessage) {
          print(errorMessage);
        }
        return ServerResponse(response);
      }
    

    }

    Before using the WebService class you must inject it as follows.
    I am using provider package for injecting.

    void main() async {
      return runApp(MultiProvider(providers: [Provider.value(value:WebService ())], child: const MyApp()));
    }
    

    Using:

    var response = await Provider<WebService>.of(context,listen:false).getFunction();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search