The included part ''xclickcounter.dart'' must have a part-of directive - dart

Create a sample web application using the Web UI (web_ui) library, e.g., mylib
open mylib.dart, make it a library:
library mylib;
import 'dart:html';
import 'package:web_ui/web_ui.dart';
part 'xclickcounter.dart';
...
open xclickcounter.dart, remove imports and insert:
part of mylib;
web/out/mylib.dart and web/out/xclickcounter.dart get messed up:
The included part ''xclickcounter.dart'' must have a part-of directive
Classes can only mixin other classes
Mixin can only be applied to class
... more errors follow
What am I doing wrong? Please help :(
Edit: if I don't edit generated sample code, wdc will generate code that falls into separate libraries:
web/out/xclickcounter.dart => x_click_counter
web/out/mylib.dart => mylib_html
Does it mean that if we use web_ui we should not create our own libraries and wdc will do this for us automatically?
Update: if I don't use any library name, similar to what generated sample code does, and only rely on the library names generated by xdc in web/out/... files, I still run into trouble when importing my two components into a 3rd file. Dart Editor will produce the following warning:
The imported libraries 'compa.dart' and 'compb.dart'
should not have the same name
The workaround is to name your libraries based on what xdc produces in web/out/... files, that is:
compa.dart => x-comp-a
compb.dart => x-comp-b
After explicitly placing components into libraries like these the Dart Editor warning disappears.

Related

correct way to import mydart.dart file in main

In my dart project projectxyz, I have a dart class declared in in myclass.dart. In main.dart, Android Studio gives two ways, both work, but I did not understand what are the pros and cons of each method:
import 'myclass.dart';
or:
import 'package:projectxyz/myclass.dart';
What is the difference in these two approaches?
That depends on how the main file itself is invoked (and where it's located).
I'll assume the main.dart library is inside the lib/ directory, because otherwise you wouldn't have the two options for importing myclass.dart.
If you invoke the main file with a file: URI, then the relative import of myclass.dart will also be imported with a file: URI. Since Dart uses the import URI to distinguish different libraries, if someone else imports myclass.dart using a package: URI, then it will be treated as two different libraries introducing different classes with the same name.
It used to be that running dart lib/main.dart would treat that as a file: URI. The Dart parser has gotten smarter about that, and now it recognizes that an entry point library in a lib/ directory should have been a package: URI, and replaces the entry point URI with package:projectxyz/main.dart.
After that, it makes no difference whether you use myclass.dart or package:projectxyz/myclass.dart.
Really, there is no difference between the two. Saying import 'myclass.dart' is high-level sugar for import 'package:projectxyz/myclass.dart';.
On the other hand, import 'myclass.dart' is easier to read and understand, and generally looks better. It also decreases confusion as to where exactly your code is being imported from, as anybody who reads this statement knows to look for the file elsewhere in your project. Because of this, you should try to use this form wherever possible.

I see an angular2 'bind' function defined in angular2/angular2.d.ts - did it used to be in 'angular2/di.d.ts?

Many of the samples I have seen for angular2 have the following import statement:
import {bind} from 'angular2/di';
I am working in VS Code (with TypeScript) and it complains about not being able to find the angular2/di module.
However I do see a bind function defined in angular2/angular2.d.ts. If I change the import statement to the following, then the error goes away.
import {bind} from 'angular2/angular2';
Is the question in the title off-base and I am making some erroneous assumption?
If not, why do many samples reference one module to import the bind function from, yet I seem to be able to get it from a different module?
Most likely because you looked at versions from older alphas. Look at the angular2.ts file. Everything is exported from it. Also note that the d.ts is going to contain everything to resolve types in your IDE and at compilation time. What import does is actually importing the .js files.

Changing the Gridpanel class in Gridworld

I am trying to follow the steps to change the Gridworld appearance according to this. I've already imported source code for my gridworld jar file; ie. I can go and look at Bug.class or Gridpanel.class if I wanted to. However, I can't edit these files to produce the results that that pdf suggests. How do I do this? Did I import the source code incorrectly?
I don't think you can edit an external resource like gridworld.jar. You are going to need to create 4 new packages:
actor
grid
gui
world
Then copy the class files from gridworld.jar packages into your corresponding ones. Now you can edit the files. Make sure you include any miscellaneous files that might also be in the gridworld.jar packages, their important.
With your own packages you no longer need the gridworld.jar external resource, and there is a difference in your import statements. Instead of
import gridworld.actor.Actor;
Do this:
import actor.Actor;
Note: You will need to change all of the import statements in the new package classes to reference the other new packages. I believe that there were also some errors, not project breaking, but I just added suppressors.

Is it possible to give a different name to a package dependency in pubspec

I managed to add a relative path dependency in my app
name: myapp
description: A sample app
dependencies:
mylib:
path: ../mylib
and then import it in my source code
import 'package:mylib/mylib.dart';
However, the name 'mylib' is taken from the library package pubspec and if I want to change it (for example 'mynewlib'), I have to change the name everywhere (pubspec AND dart source code)
It also prevent having 2 packages with the same name (yes I know, weird, but I don't control what people put in pub.dartlang.org). What I'd like to do is something like
name: myapp
description: A sample app
dependencies:
mylib:
path: ../mylib
name: mynewlib
and have in source code
import 'package:mynewlib/mylib.dart';
However I cannot find the proper syntax and whether that's possible or not. (Sample code ready for testing is here: https://github.com/alextekartik/dart-test/tree/master/lib_test). To note that here I'm not talking about library name but package name (and naming the package mylib can be confusing)
There is no way to define another name for a package itself (as far as I know - if there is, I'd be interested too).
However, as a workaround, you could rewrap it. For example, let's assume you have two "mylib" packages.
Create a new library application "mylib1". There, you import the first "mylib" and reexport it using export. Create another library application "mylib2" for the other "mylib". Then you have different package names to use in the same application.
Yes, it is kinda awkward, but as long as there is no better way...
You can resolve name conflicts at import with:
import 'package:mylib/mylib.dart' as Foo;
This will create a top level name to access the library API:
Foo.bar();

dartEditor file hierarchy and library/part logic

When I create a 'Web Application' with DartEditor in Windows8, it gives me this hierarchy
ClientView/
packages/
pubspec.lock
pubspec.yaml
web/
packages/
clientview.css
clientview.dart
clientview.dart.js
clientview.dart.js.deps
clientview.dart.js.map
clientview.html
I then add the lib/ file:
web/
...
lib/
src/
canvas.dart
i_drawable.dart
node.dart
client_canvas.dart
...
client_canvas.dart file
library client_view;
import 'dart:html';
import 'package:meta/meta.dart';
// Interface
part 'src/i_drawable.dart';
// Class
part 'src/canvas.dart';
part 'src/node.dart';
In each of the files included after the 'part' keyword, I've added this line:
part of client_view;
But it seems that none of the classes can be accessed by the other dart code
class Canvas implements IDrawable // no such type 'IDrawable'
abstract class Node implements IDrawable // no such type 'IDrawable'
class CustomNode extends Node // no such type 'Node' (if try to create a custom node)
I guess it's the way pubs and library are organized that I've not understood yet.
So what I tried to do is to create a library, inside the same project, I'd like to import that library inside the clientview.dart file that is called by the clientview.html file.
Your help would be greatly appreciated!
Since you're using relative imports, and everything appears to be part of the same library, I see no obvious reason for the errors. Maybe you can share some more details.
You should probably reorganize your source tree to be more inline with Pub standards though. web/ and lib/ should both be top level directories, otherwise you won't be able to do package: imports of your own libraries. This is one reason why it's recommended that web/ only contain entry points (scripts with a main() method) and everything else should be in lib/.
Also, I would try to use part very sparingly. I find it much better to define almost every file as a library and just import them. It makes dependencies for each file much more clear and allows consumers to only import the interfaces, and not the implementations. export makes it possible to build a library that exposes the definitions of another.
Finally, a style nit: Dart isn't C# and we don't use that ugly 'I' prefix on our interfaces :-) Just name your interface Drawable.

Resources