flutter cookie保存

使用插件cookie_jar,插件的具体使用不赘述
创建一个my_cookie.dart的类

import 'dart:io';

import 'package:cookie_jar/cookie_jar.dart';
import 'package:path_provider/path_provider.dart';

class MyCookie {
  static String? _cookiePath;
  static Future get cookiePath async {
    if (_cookiePath == null) {
      Directory appDocDir = await getApplicationDocumentsDirectory();
      _cookiePath = appDocDir.path;
    }
    return _cookiePath!;
  }

  static PersistCookieJar? _cookieJar;
  static Future get cookieJar async {
    if (_cookieJar == null) {
      String path = await cookiePath;
      _cookieJar = PersistCookieJar(storage: FileStorage(path));
    }
    return _cookieJar!;
  }
}

dio拦截器中加入cookieManager
interceptors.add(CookieManager(await MyCookie.cookieJar));
完成!

你可能感兴趣的:(flutter cookie保存)