dio icon indicating copy to clipboard operation
dio copied to clipboard

BrowserHttpClientAdapter() error in web.

Open JemmyWang94 opened this issue 1 year ago • 6 comments

Package

dio

Version

5.6.0

Operating-System

Web

Adapter

Default Dio

Output of flutter doctor -v

No response

Dart Version

3.5.0

Steps to Reproduce

BrowserHttpClientAdapter adapter = BrowserHttpClientAdapter(); adapter.withCredentials = true; dio.httpClientAdapter = adapter;

Expected Result

no error and work well.

Actual Result

TypeError: Cannot read properties of undefined (reading 'new') ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ │ #0 fortex7/packages/fortexmobile/common/network/web/web_platform.dart.lib.js 18:57 createClientAdapter

in flutter 3.22.3, it work well

JemmyWang94 avatar Aug 14 '24 08:08 JemmyWang94

flutter: 3.24.0

JemmyWang94 avatar Aug 14 '24 08:08 JemmyWang94

Could you share the resolved version of dio and dio_web_adapter in your pubspec.lock?

AlexV525 avatar Aug 15 '24 11:08 AlexV525

Hi bro, I also faced this error:

  dio:
    dependency: "direct main"
    description:
      name: dio
      sha256: "0dfb6b6a1979dac1c1245e17cef824d7b452ea29bd33d3467269f9bef3715fb0"
      url: "https://pub.dev"
    source: hosted
    version: "5.6.0"
  dio_web_adapter:
    dependency: transitive
    description:
      name: dio_web_adapter
      sha256: "33259a9276d6cea88774a0000cfae0d861003497755969c92faa223108620dc8"
      url: "https://pub.dev"
    source: hosted
    version: "2.0.0"

Dan-Crane avatar Aug 16 '24 06:08 Dan-Crane

I'm getting the normal result using the example in the repo: https://github.com/cfug/dio/tree/main/example_flutter_app

Could someone provide a standalone runnable minimal reproducible example?

AlexV525 avatar Aug 16 '24 07:08 AlexV525

Here is the code where I am encountering the error: link to code.

Dan-Crane avatar Aug 16 '24 08:08 Dan-Crane

It looks like a Dart bug, filed dart-lang/sdk#56498.

Meanwhile, a workaround could be:

HttpClientAdapter makeHttpClientAdapter() {
  final adapter = HttpClientAdapter() as BrowserHttpClientAdapter;
  adapter.withCredentials = true;
  return adapter;
}

AlexV525 avatar Aug 16 '24 11:08 AlexV525

Workaround stopped working for me, this worked:

class _Adapter extends BrowserHttpClientAdapter {
  _Adapter() {
    withCredentials = true;
  }
}

ltOgt avatar Jan 02 '25 11:01 ltOgt

Workaround stopped working for me, this worked:

class _Adapter extends BrowserHttpClientAdapter { _Adapter() { withCredentials = true; } }

How do you extend BrowserHttpClientAdapter? I get Uncaught TypeError when I run my app. Here is the complete code:

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

class CustomBrowserHttpClientAdapter extends BrowserHttpClientAdapter {
  CustomBrowserHttpClientAdapter() {
    withCredentials = true;
  }
}

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final Dio dio = Dio()..httpClientAdapter = CustomBrowserHttpClientAdapter();

    return Container();
  }
}

And here is the console output:

Uncaught TypeError: Class extends value undefined is not a constructor or null
    at load__packages__didgah__main_test_dart (main_test.dart.lib.js:79:13)

misaghE avatar Jan 19 '25 18:01 misaghE

Hi guys i have the same problem with:

flutter: 3.24.3 dart: 3.5.3 Dio: 5.7.0

class HttpClientBrowser with DioMixin implements HttpClient {
  HttpClientBrowser(HttpSettings settings) {
    options = settings.baseOptions;
    httpClientAdapter = BrowserHttpClientAdapter();
    interceptors.addAll(settings.interceptors);
  }
}

any update on the bug?

iPypeNB avatar Jan 30 '25 03:01 iPypeNB

The thread is locked until https://github.com/dart-lang/sdk/issues/56498 gets resolved.

AlexV525 avatar Jan 30 '25 04:01 AlexV525