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

conditional import for code base that is also used for mobile? #111

Closed
jdeltoft opened this issue Apr 27, 2020 · 3 comments
Closed

conditional import for code base that is also used for mobile? #111

jdeltoft opened this issue Apr 27, 2020 · 3 comments
Labels
type-question A question about expected behavior or functionality

Comments

@jdeltoft
Copy link

I'm importing the html version like this:

import 'package:web_socket_channel/html.dart';

and then I'm using this call to create the channel:

        channel = HtmlWebSocketChannel.connect(
            'wss://my.domain.com:1234/ws');

But when I build for mobile, is there a way to condition this out? I am familiar with using kIsWeb for calls I only want on web, and I have one single code base before this above need to use websockets.

Am I doing something wrong?

@jdeltoft
Copy link
Author

Flutter doctor -v:

[✓] Flutter (Channel beta, v1.17.0, on Mac OS X 10.14.6 18G3020, locale en-US)
• Flutter version 1.17.0 at /Users/jeltoft/flutter/flutter
• Framework revision d3ed9ec945 (3 weeks ago), 2020-04-06 14:07:34 -0700
• Engine revision c9506cb8e9
• Dart version 2.8.0 (build 2.8.0-dev.18.0 eea9717938)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/jeltoft/Library/Android/sdk
• Platform android-28, build-tools 28.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.3.1, Build version 11C504
• CocoaPods version 1.9.1

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

[✓] Android Studio (version 3.6)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 45.1.1
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] Connected device (3 available)
• SM G965U • 3555453044323098 • android-arm64 • Android 10 (API 29)
• Chrome • chrome • web-javascript • Google Chrome 81.0.4044.122
• Web Server • web-server • web-javascript • Flutter Tools

• No issues found!

Error starts like this:

Compiler message:
../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-1.1.0/lib/html.dart:8:8: Error: Not found: 'dart:html'
import 'dart:html';
^
../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-1.1.0/lib/html.dart:21:9: Error: Type 'WebSocket' not found.
final WebSocket _webSocket;
^^^^^^^^^
../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-1.1.0/lib/html.dart:21:9: Error: 'WebSocket' isn't a type.
final WebSocket _webSocket;
^^^^^^^^^
../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-1.1.0/lib/html.dart:67:14: Error: The method 'WebSocket' isn't defined for the class 'HtmlWebSocketChannel'.

  • 'HtmlWebSocketChannel' is from 'package:web_socket_channel/html.dart' ('../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-1.1.0/lib/html.dart').
    Try correcting the name to the name of an existing method, or defining a method named 'WebSocket'.
    : this(WebSocket(url.toString(), protocols)
    ^^^^^^^^^
    ../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-1.1.0/lib/html.dart:74:34: Error: The getter 'WebSocket' isn't defined for the class 'HtmlWebSocketChannel'.
  • 'HtmlWebSocketChannel' is from 'package:web_socket_channel/html.dart' ('../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-1.1.0/lib/html.dart').
    Try correcting the name to the name of an existing getter, or defining a getter or field named 'WebSocket'.
    if (_webSocket.readyState == WebSocket.OPEN) {
    ^^^^^^^^^

@natebosch
Copy link
Member

You can conditionally switch an import from one URI to another, but you can't conditionally change any code. HtmlWebSocketChannel.connect will never work outside the web.

Luckily there is WebSocketChannel.connect which handles picking the correct behavior on web vs IO. You should be able to switch you import from package:web_socket_channel/html.dart to package:web_socket_channel/web_socket_channel.dart and change HtmlWebSocketChannel to WebSocketChannel.

@natebosch natebosch added the type-question A question about expected behavior or functionality label Apr 27, 2020
@jdeltoft
Copy link
Author

@natebosch thanks!! I needed to add URI.parse() around the input string. Perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

2 participants