Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dio caching issue with identical ListParam objects #2364

Open
suojae opened this issue Jan 26, 2025 · 0 comments · May be fixed by #2366
Open

Dio caching issue with identical ListParam objects #2364

suojae opened this issue Jan 26, 2025 · 0 comments · May be fixed by #2366
Labels
p: dio Targeting `dio` package s: feature This issue indicates a feature request

Comments

@suojae
Copy link

suojae commented Jan 26, 2025

Package

dio

Version

5.7.0

Operating-System

iOS, Web, Android, MacOS, Linux, Windows

Adapter

Default Dio

Output of flutter doctor -v

jayoengchoi@jayoengs-MacBook-Pro lib % flutter doctor -v
[✓] Flutter (Channel stable, 3.27.1, on macOS 14.5 23F79 darwin-arm64, locale en-KR)
    • Flutter version 3.27.1 on channel stable at /Users/jayoengchoi/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 17025dd882 (6 weeks ago), 2024-12-17 03:23:09 +0900
    • Engine revision cb4b5fff73
    • Dart version 3.6.0
    • DevTools version 2.40.2

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/jayoengchoi/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

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

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.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 17.0.11+0-17.0.11b1207.24-11852314)

[✓] Connected device (4 available)
    • Jonghyuck’s iPhone (mobile)     • 00008101-000408410A82001E • ios            • iOS 17.5.1 21F90
    • macOS (desktop)                 • macos                     • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad     • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                    • web-javascript • Google Chrome 131.0.6778.265

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

• No issues found!

Dart Version

3.6.0

Steps to Reproduce

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dio Test',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Dio Request Test')),
      body: Center(
        child: ElevatedButton(
          onPressed: _testDioCaching,
          child: const Text('Test Dio Request Caching'),
        ),
      ),
    );
  }

  void _testDioCaching() async {
    final dio = Dio();
    final cachedRequests = <ListParam, Response>{};

    // Define two identical requests with the same ListParam
    final param1 = ListParam(['item1', 'item2'], ListFormat.csv);
    final param2 = ListParam(['item1', 'item2'], ListFormat.csv);

    // Cache a response for param1
    final response = await dio.get('https://jsonplaceholder.typicode.com/posts/1');
    cachedRequests[param1] = response;

    // Check if param2 is considered the same as param1 in the cache
    if (cachedRequests.containsKey(param2)) {
      debugPrint('Cached response used for param2');
    } else {
      debugPrint('New request sent for param2');
    }
  }
}
  1. This example uses the Dio package to simulate an API request and attempts to cache the response.
  2. Two identical ListParam instances (param1 and param2) with the same contents are created.
  3. The param1 request is cached in a map, and the code checks whether the param2 request can retrieve the cached response.
  4. If the two ListParam instances are not treated as equal, the code logs New request sent for param2.

Expected Result

Image

When running the test with two identical ListParam instances (param1 and param2) that have the same contents but different references, the application should correctly identify that these instances are equal. As a result, the cached response for param1 should be used for param2, and the following message should appear in the console:

Cached response used for param2

This confirms that the caching mechanism correctly handles deep equality for ListParam instances with identical contents.

Actual Result

Image

When running the test with two identical ListParam instances (param1 and param2) that have the same contents but different references, the application incorrectly treats these instances as different. As a result, the following message appears multiple times in the console:

New request sent for param2
New request sent for param2
New request sent for param2

This indicates that the caching mechanism is not recognizing ListParam instances with identical contents as equal, leading to repeated requests instead of using the cached response.

@suojae suojae added h: need triage This issue needs to be categorized s: bug Something isn't working labels Jan 26, 2025
@AlexV525 AlexV525 added s: feature This issue indicates a feature request p: dio Targeting `dio` package and removed h: need triage This issue needs to be categorized s: bug Something isn't working labels Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: dio Targeting `dio` package s: feature This issue indicates a feature request
Projects
None yet
2 participants