Site-www: Conditional imports is not covered in the language tour

Created on 6 May 2019  路  14Comments  路  Source: dart-lang/site-www

Page URL: https://www.dartlang.org/guides/language/language-tour
Page source: src/_guides/language/language-tour.md.

Conditional imports is not covered by the language tour. Conditional imports are usually done like:

import 'src/impl_stub.dart'  // stub implementation
    if (dart.library.io) 'src/impl_io.dart' // implementation using dart:io
    if (dart.library.html) 'src/impl_web.dart'; // implementation using dart:html|js

It would be nice to cover this in the language tour as the specification is hard to read. A few good examples of what you can make conditions on would also be useful, as this is not obvious.

@lrn, please correct me if this is not a stable language feature?

LanguageTour docs e1-hours p1-high

Most helpful comment

This is a shipped and specified feature, and there are no current plans to deprecate it.

All 14 comments

@kwalrath what the needs-info part? From the language teams perspective this is a fully supported feature.

I think I was waiting to hear if this was a stable feature.

Hmm, I might have pinged the wrong LRN, sorry..

@lrhn, is this something that is stable, and can we encourage developers to adopt it?

Related issue (where I just commited to getting more info from lang. team): https://github.com/dart-lang/site-www/issues/1750

here is an example project that i use this with for desktop, mobile and web: https://github.com/AppleEducate/moor_shared

Great example @AppleEducate, it solved my issue in https://github.com/flutter/flutter/issues/40488

No problem 馃槑馃挭馃徎

This is a shipped and specified feature, and there are no current plans to deprecate it.

This still requires documentation. I'm finding problems with conditional imports.

This is my firebase_access.dart class

export 'unsupported_firebase.dart' if (dart.library.io) 'firebase_mobile.dart' if (dart.library.html) 'firebase_web.dart';

This class works for firebase_mobile.dart but does NOT work for firebase_web.dart.
I don't know why, I tried printing a line for each, it prints for firebase_mobile on mobile, does NOT print for firebase_web.

This is firebase_web.dart (using the firebase_web plugin) :
image
In the following code, we have

    print("firebase web init");

while in firebase_mobile.dart (using the official firebase plugin):
image
we have

    print("firebase mobile init");

Now in my main class, I'm doing the following:
image

Note the import firebase_access.dart

In both mobile and web, it prints beginning init
However on mobile, it prints firebase mobile init after while on web, it does NOT print firebsae web init

I'm unsure why. The lack of documentation is problem.

EDIT: Also want to mention: breakpoint obviously isn't running in the firebase_web.dart class.

Here's my class hierarchy as evidence that it's not a spelling error:
image

cc @lrhn

I see no problem with the conditional export. My guess is that the problem lies elsewhere.
If the program compiles, then it has exported some version of the library.
Does the unsupported_firebase.dart file contain any prints, so you can see if it picks that incorrectly?

@lrhn Yes it does:

image

It's never printed.

Should I open a new issue for this?

Yes, please open a new issue (the present bug tracked documentation), with the smallest set of repro steps you have. Please open it in the SDK repo: https://github.com/dart-lang/sdk/issues

Was this page helpful?
0 / 5 - 0 ratings

Related issues

feinstein picture feinstein  路  4Comments

rahul-bavaliya picture rahul-bavaliya  路  3Comments

dev-aentgs picture dev-aentgs  路  3Comments

sarahec picture sarahec  路  3Comments

Haugen picture Haugen  路  4Comments