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

[web] [DDC] TypeError: Cannot read properties of undefined (reading 'new') #56498

Open
AlexV525 opened this issue Aug 16, 2024 · 15 comments
Open
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler

Comments

@AlexV525
Copy link
Contributor

AlexV525 commented Aug 16, 2024

This tracker is for issues related to:

  • Dart native and web compilers
  • Dart VM

Upstream: cfug/dio#2282

We've split our Dio adapter with conditional imports. However, writing the caller in a specific way does not reference the correct constructor as I can tell.

Consider the example, running this on Flutter Web environment will unexpectedly throw TypeError: Cannot read properties of undefined (reading 'new'):

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

import 'adapter.dart';

void main() {
  try {
    final adapter = makeHttpClientAdapter();
    print(adapter);
  } on Object catch (error) {
    print(error);
  }

  runApp(const MyApp());
}
adapter.dart
import 'package:dio/dio.dart';

import 'client_adapter_io.dart'
    if (dart.library.js_interop) 'client_adapter_web.dart' as adapter;

HttpClientAdapter makeHttpClientAdapter() => adapter.makeHttpClientAdapter();
adapter_io.dart
import 'package:dio/dio.dart';
import 'package:dio/io.dart';

HttpClientAdapter makeHttpClientAdapter() {
  return IOHttpClientAdapter();
}
adapter_web.dart
import 'package:dio/browser.dart';
import 'package:dio/dio.dart';

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

Workaround

HttpClientAdapter makeHttpClientAdapter() {
  final adapter = HttpClientAdapter() as BrowserHttpClientAdapter;
  adapter.withCredentials = true;
  return adapter;
}
@dart-github-bot
Copy link
Collaborator

Summary: The issue is a TypeError thrown when using conditional imports with Dio on Flutter Web. The error occurs because the correct constructor for the HttpClientAdapter is not being referenced due to the way the caller is written.

@dart-github-bot dart-github-bot added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Aug 16, 2024
@AlexV525
Copy link
Contributor Author

@Rexios80 Do you have any clues what is going on here? 🤔

@Rexios80
Copy link
Contributor

What dart/flutter version? There might be some fixes that still aren't merged into stable.

@Dan-Crane
Copy link

@Rexios80 im faced this:

[✓] Flutter (Channel stable, 3.22.2, on macOS 14.5 23F79 darwin-arm64, locale en-RU)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] VS Code (version 1.92.2)
[✓] Connected device (5 available)
[✓] Network resources

@Rexios80
Copy link
Contributor

Can you reproduce on flutter master?

@Dan-Crane
Copy link

This also happens on master 3.24.0-1.0.pre.606

@Rexios80
Copy link
Contributor

Is it the cascade that's causing the issue?

@AlexV525
Copy link
Contributor Author

Is it the cascade that's causing the issue?

Nope. Removing it doesn't help.

@Rexios80
Copy link
Contributor

You say this is a workaround:

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

What about this fixes the issue if it's not removing the cascade? Is it the cast?

@AlexV525
Copy link
Contributor Author

You say this is a workaround:

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

What about this fixes the issue if it's not removing the cascade? Is it the cast?

Yes, creating the adapter like this and force cast it does work, it's not about the cascade. The workaround is to help people to achieve their goal.

@nshahan
Copy link
Contributor

nshahan commented Aug 16, 2024

Which of the web environments are you experiencing this issue?

  • Production JavaScript (dart2js compiler)
  • Production WASM (dart2wasm compiler)
  • Development (DDC compiler)

@nshahan nshahan added the needs-info We need additional information from the issue author (auto-closed after 14 days if no response) label Aug 16, 2024
@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Aug 16, 2024
@AlexV525
Copy link
Contributor Author

Which of the web environments are you experiencing this issue?

  • Development (DDC compiler)

The DDC compiler. The built Web application does not run into the issue.

@AlexV525 AlexV525 changed the title [web] TypeError: Cannot read properties of undefined (reading 'new') [web] [DDC] TypeError: Cannot read properties of undefined (reading 'new') Aug 17, 2024
@github-actions github-actions bot removed the needs-info We need additional information from the issue author (auto-closed after 14 days if no response) label Aug 17, 2024
@sigmundch
Copy link
Member

@nshahan - I recall we've seen an error with this message before long ago, but we didn't have a repro at hand. Maybe they are related? (see #50380)

@Rexios80
Copy link
Contributor

I actually have this error in one of my projects now. Not sure how it took me this long to notice. The workaround provided by @AlexV525 does fix the issue.

@AlexV525
Copy link
Contributor Author

This is getting worse according to cfug/dio#2282 (comment)

@nshahan @sigmundch Would you mind taking further investigations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler
Projects
None yet
Development

No branches or pull requests

7 participants