Forward package member in dart - dart

I import a package and would forward a member of it. What syntax does Dart offer for that?
foo.dart
import 'package:xzy/xzy.dart'; // contains class Xyz
// how do I forward Xyz here to make it available in bar.dart?
bar.dart
import 'package:mypackage/foo.dart';
Xyz is hidden here

You can use export
import 'package:xzy/xzy.dart';
export 'package:xzy/xzy.dart' show Xyz;
or just
export 'package:xzy/xzy.dart' show Xyz;
You don't need to import for that. Just export is enough if you don't need Xyz in this re-exporting file.
See also
https://www.dartlang.org/guides/libraries/create-library-packages
What is the difference between "show" and "as" in an import statement?

Related

Does Dart have import alias?

I found myself written tedious code when importing files into dart files like the following:
import '../../constants.dart';
I'm wondering if there is any way to create an alias to specific folder like:
import '#shared/constants.dart';
Thanks,
Javi.
Dart doesn't allow you to rename imported identifiers, but it allows you to specify an import prefix
import '../../constants.dart' as foo;
...
foo.ImportedClass foo = foo.ImportedClass();
It allows also to filter imported identifiers like
import '../../constants.dart' show foo hide bar;
See also
https://www.dartlang.org/guides/language/language-tour#libraries-and-visibility
What is the difference between "show" and "as" in an import statement?
Barrel files can also make importing easier like
lib/widgets/widgets.dart
export 'widget1.dart';
export 'widget2.dart';
export 'widget3.dart';
export 'widget4.dart';
lib/pages/page1.dart
import '../widgets/widgets.dart';
Widget build(BuildContext context) => Widget1();
No. Dart do not have import alias.
But you have absolute imports which makes up for it:
import 'package:my_lib/shared/constants.dart
This will import the file /lib/shared/constants.dart

Could not load lang::java::jdt::Java

When trying to import JAVA modules I receive an error:
// JAVA imports
import lang::java::jdt::Java;
import lang::java::jdt::JDT;
import lang::java::jdt::JavaADT;
Could not load lang::java::jdt::Java
In the console:
rascal>import lang::java::jdt::Java;
|prompt:///|(0,29,<1,0>,<1,29>): Could not import module lang::java::jdt::Java: can not find in search path
Advice: |http://tutor.rascal-mpl.org/Errors/Static/ModuleImport/ModuleImport.html|
I'm using Eclipse and am trying to use the AstNode datatype. Any ideas?
The JDT modules have been replaced a while back by the m3 model.
While they are a bit different, the AST part should be comparable.
Check the m3 ast documentation and the Measuring java recipe.
Is this an old project you are trying to get up and running?

How to use HashLocationStrategy for router in angular2-beta.20 in dart?

I tried to use the router in angular2-beta.20 in Dart with the HashLocationStrategy.
But I couldn't find any docs, except for
this link to angular2-beta.15 docs, which are incomplete.
The example shows TypeScript imports instead of Dart ones.
So I tried to import package:angular2/router.dart, but the Dart Analyzer keeps complaining that it can not find LocationStrategy and HashLocationStrategy
Also I don't know, how to write the import exactly, because a top-level provide function, as in the example above, seems non existent.
provide(LocationStrategy, {useClass: HashLocationStrategy})
After some research I found the following:
LocationStrategy and HashLocationStrategy are now part of
package:angular2/platform/common.dart instead of package:angular2/router.dart.
The bootstrap()- method is platform specific, so we need to import package:angular2/platform/browser.dart.
We need to import package:angular2/router.dart to have ROUTER_PROVIDERS available in bootstrap() method.
Here is a working code example for the dart file initializing :
// needed to import "bootstrap" method
import 'package:angular2/platform/browser.dart';
// needed to import LocationStrategy and HashLocationStrategy
import 'package:angular2/platform/common.dart';
// needed for Provider class
import 'package:angular2/angular2.dart';
// needed to import ROUTER_PROVIDERS
import 'package:angular2/router.dart';
// import your app_component as root component for angular2
import 'app_component.dart';
void main() {
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
const Provider(LocationStrategy, useClass: HashLocationStrategy)
]);
}
Hope this helps somebody! :)

connectWithResult method is undefined for MqttClient object

Below are my imports used in my java program. What I am trying to do is, to create a client of type MqttCient and then use the method connectWithResuls as follows
client.connectWithResult(opts);
the problem is eclipse underscores the lie mentioned above with red squiggle, and I do not know why, I also referred to Paho java docs here.
please let me know why the aforementioned method is not recognised.
Imports
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;

Top level routines of one library are inaccessible when loaded through another library in Dart

Say, I load this script in the browser:
<script src='app.dart' type='application/dart'></script>
Now, in app.dart I have this:
import 'library1.dart';
unleashTheKraken();
Then in library1.dart you'll find this:
library library1;
import 'library2';
And finally in library2.dart we'll have:
library library2;
unleashTheKraken() => print('Unleashing the Kraken')
And the result is: Exception: No top-level method 'unleashTheKraken' declared. How so?
Because imports don't chain automatically. You have to use the export statement for that.
library library1;
import "library2.dart";
export "library2.dart";
And to avoid unnecessary code: import and export are completely independent. If you don't use unleashTheKraken in library1 itself, you can omit the import statement and just use export alone.

Resources