Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Question][CookieManager] About override the old cookies by new cookies when they have the same keys? #2442

@namnh-0652

Description

@namnh-0652

Package

cookie_manager

Version

3.3.0

Operating-System

iOS

Adapter

Default Dio

Output of flutter doctor -v

[!] Flutter (Channel stable, 3.32.7, on macOS 15.3.1 24D70 darwin-arm64, locale en-VN) [2.9s]
    • Flutter version 3.32.7 on channel stable at /Users/myname/Desktop/Development/SDKs/flutter
    ! Upstream repository unknown source is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
    • Framework revision d7b523b356 (9 days ago), 2025-07-15 17:03:46 -0700
    • Engine revision 39d6d6e699
    • Dart version 3.8.1
    • DevTools version 2.45.1
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git"
      directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [7.8s]
    • Android SDK at /Users/myname/Library/Android/sdk
    • Platform android-36, build-tools 36.0.0
    • ANDROID_HOME = /Users/myname/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/myname/Library/Android/sdk
    • Java binary at: /Users/myname/.CICD/JDK/jdk-17.jdk/Contents/Home/bin/java
      This JDK is specified in your Flutter configuration.
      To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2) [7.9s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [31ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2025.1.2) [31ms]
    • Android Studio at /Applications/Android Studio Preview.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13391695-b895.109)

[✓] Android Studio (version 2024.2) [30ms]
    • Android Studio at /Applications/Android Studio 3.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.5+-12932927-b750.29)

[✓] IntelliJ IDEA Community Edition (version 2025.1.2) [29ms]
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.102.2) [11ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.114.0

[✓] Connected device (3 available) [12.6s]
    • iPhone 15 (mobile) • 9142A079-C987-4702-9215-9E21E12C28DC • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-17-4 (simulator)
    • macOS (desktop)    • macos                                • darwin-arm64   • macOS 15.3.1 24D70
      darwin-arm64
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome
      138.0.7204.169

[✓] Network resources [502ms]
    • All expected network resources are available.

! Doctor found issues in 1 category.

Dart Version

3.8.1

Steps to Reproduce

In loadCookies function, why don’t we override the old cookie with the new cookie value when the keys are the same?

Future<String> loadCookies(RequestOptions options) async {
  final savedCookies = await cookieJar.loadForRequest(options.uri);
  final previousCookies =
      options.headers[HttpHeaders.cookieHeader] as String?; // <== (1)
  final cookies = getCookies([
    ...?previousCookies
        ?.split(';')
        .where((e) => e.isNotEmpty)
        .map((c) => Cookie.fromSetCookieValue(c)),
    ...savedCookies,
  ]);
  return cookies;
}

1- When does case (1) happen? In most case, I debugged, its value is null, but my in production, sometime, cookies have duplicated values, is this the cause?

2- Am I correct in thinking that if savedCookies contain the same keys as previousCookies, the old values will not be overridden, and the returned cookies will contain duplicate keys?

For example:

previousCookies = 'a=1; b=2';
savedCookies   = 'a=3; b=4';

// Result:
cookies = 'a=1; a=3; b=2; b=4';

3- Last, if above are correct, is there a case which I need to check to prevent duplicated cookies?

Expected Result

N/A

Actual Result

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    h: need triageThis issue needs to be categorizeds: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions