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

The getter 'cookieJar' isn't defined for the type 'Dio'. #1941

Closed
PascalMalz opened this issue Aug 17, 2023 · 5 comments
Closed

The getter 'cookieJar' isn't defined for the type 'Dio'. #1941

PascalMalz opened this issue Aug 17, 2023 · 5 comments
Labels
i: wontfix This will not be worked on

Comments

@PascalMalz
Copy link

PascalMalz commented Aug 17, 2023

Package

dio

Version

^5.3.2

Output of flutter doctor -v

PS C:\Users\pam\self_code> flutter doctor -v
[!] Flutter (Channel stable, 3.13.0, on Microsoft Windows [Version 10.0.22621.2134], locale en-US)
    • Flutter version 3.13.0 on channel stable at C:\src\flutter
    ! Warning: `flutter` on your path resolves to C:\Users\pam\self_code\flutter, which is not inside your current Flutter SDK checkout at
      C:\src\flutter. Consider adding C:\src\flutter\bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision efbf63d9c6 (2 days ago), 2023-08-15 21:05:06 -0500
    • Engine revision 1ac611c64e
    • Dart version 3.1.0
    • DevTools version 2.25.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and     
      upgrades.

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at C:\Users\pam\AppData\Local\Android\sdk
    • Platform android-33, build-tools 33.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.5.5)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.5.33627.172
    • Windows 10 SDK version 10.0.20348.0

[√] Android Studio (version 2022.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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.6+0-b2043.56-9586694)

[√] IntelliJ IDEA Community Edition (version 2020.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3
    • 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.81.1)
    • VS Code at C:\Users\pam\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (4 available)
    • SM G991B (mobile) • R3CRB0NP1WJ • android-arm64  • Android 11 (API 30)
    • Windows (desktop) • windows     • windows-x64    • Microsoft Windows [Version 10.0.22621.2134]
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 115.0.5790.171
    • Edge (web)        • edge        • web-javascript • Microsoft Edge 115.0.1901.203

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

! Doctor found issues in 1 category.

Dart Version

3.1.0

Steps to Reproduce

This code throws an error "The getter 'cookieJar' isn't defined for the type 'Dio'.":

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:cookie_jar/cookie_jar.dart';
import 'package:http_parser/http_parser.dart';
import '../models/AudioFile.dart';

class MusicApi {
  final String baseUrl = 'ip';
  final String apiUrl = 'ip/.../.../';
  late final Dio _dio;

  MusicApi() {
    final cookieJar = CookieJar();
    _dio = Dio();
    _dio.interceptors.add(CookieManager(cookieJar));
  }
  Future<void> deleteMusic(String musicId, String fileName,
      {required void Function() onSuccess, required void Function(String) onError}) async {
    final cookieJar = CookieJar();
    _dio = Dio();
    _dio.interceptors.add(CookieManager(cookieJar));
    try {
      final csrfCookie = _dio.cookieJar.loadForRequest(Uri.parse(baseUrl)).firstWhere(
            (cookie) => cookie.name == 'csrftoken',
        orElse: () => null,
      );

      if (csrfCookie == null) {
        onError('CSRF token not found');
        return;
      }

      final csrfToken = csrfCookie.value;

      // Delete record from the database
      final deleteResponse = await _dio.delete(
        '$baseUrl/$musicId',
        options: Options(
          headers: {
            'X-CSRFToken': csrfToken,
          },
        ),
      );

      if (deleteResponse.statusCode == 200) {
        // Delete associated file
        final file = File(fileName);
        if (await file.exists()) {
          await file.delete();
        }

        onSuccess();
      } else {
        onError('Error deleting music: ${deleteResponse.statusMessage}');
      }
    } catch (e) {
      onError('Error deleting music: $e');
    }
  }
}

Expected Result

No error

Actual Result

The getter 'cookieJar' isn't defined for the type 'Dio'.

@PascalMalz PascalMalz added h: need triage This issue needs to be categorized s: bug Something isn't working labels Aug 17, 2023
@kuhnroyal
Copy link
Member

What makes you think this would work?

@PascalMalz
Copy link
Author

PascalMalz commented Aug 18, 2023

It was not meant to work as is, it was just a code snippet. I just wanted to get rid of the error, I mentioned. I updated the code to show you how I want to use it. Can you tell me why it does not work this way? Or what I have to do to make it work?
This line is throwing the error: final csrfCookie = _dio.cookieJar.loadForRequest(Uri.parse(baseUrl)).firstWhere(

@kuhnroyal
Copy link
Member

Well there is just no getter or field named cookieJar so this will not work. You created your CookieJar instance 4 lines above, you can use that one.

Or you can create an extension yourself if you require that field to be there.

@AlexV525
Copy link
Member

Use your declared cookieJar rather than _dio.cookieJar since Dio has no any instance getter with .cookieJar. I'm not sure what makes you think it has but you'll need to understand the instance relationships first.

Closing as won't fix.

@AlexV525 AlexV525 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 18, 2023
@AlexV525

This comment was marked as duplicate.

@AlexV525 AlexV525 added i: wontfix This will not be worked on and removed h: need triage This issue needs to be categorized s: bug Something isn't working labels Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i: wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants