Dart library and parts are not bi-directionally resolved - dart

I have a library, 'foo', which consists of a part, 'bar', and imports the dart:io library. The library and the part are declared as follows, in 'foo.dart':
// in foo.dart
library foo;
import 'dart:io';
part "bar.dart"; // resolves
class Foo {
Bar bar = new Bar(); // resolves
HttpServer server = new HttpServer(); // resolves
}
'foo.dart' and 'bar.dart' are located in the same directory.
'bar.dart' refers back to library foo, like so:
// in bar.dart
part of foo; // does not resolve
class Bar {
Foo foo = new Foo(); // does not resolve
HttpServer server = new HttpServer // does not resolve
}
But in Dart Editor, I see a warning in 'bar.dart', saying 'Cannot resolve foo'. Accordingly, any imports (such as 'dart:io') I declare in foo.dart are not available to bar.dart, nor are any of my foo.dart definitions, and since bar.dart is a part of foo, it is not permitted to import any further libraries or declare any further parts.
I would like to organize my code in such a way that:
* any imports I bring into foo.dart to be available to bar.dart (and other source parts)
* library definitions made in one library source part are available to other library source parts.
* (obviously) library definitions made in bar.dart are available to foo.dart (this already works as expected)
Can someone please help me understand how to accomplish these?

I think this is just a bug in the Editor. Have you tried running your program? It should work.
The Editor still gets stuck warnings sometimes, and closing the file or closing the Editor for really stuck ones, usually clears it up. I'll see if I find the bug report so you can star it or maybe add a log file if it keeps happening to you.

Related

How to reflect Dart library name?

Is there a way to reflect specific library properties (like the library name) in Dart?
How do you get the library object reference?
First of all, not all Dart libraries have names. In fact, most don't. They used to, but the name isn't used for anything any more, so most library authors don't bother adding a name.
To do reflection on anything, including libraries, you need to use the dart:mirrors library, which does not exist on most platforms, including the web and Flutter.
If you are not running the stand-alone VM, you probably don't have dart:mirrors.
With dart:mirrors, you can get the program's libraries in various ways.
library my.library.name;
import "dart:mirrors";
final List<LibraryMirror> allLibraries =
[...currentMirrorSystem().libraries.values];
void main() {
// Recognize the library's mirror in *some* way.
var someLibrary = allLibraries.firstWhere((LibraryMirror library) =>
library.simpleName.toString().contains("name"));
// Find the library mirror by its name.
// Not great if you don't know the name and want to find it.
var currentLibrary = currentMirrorSystem().findLibrary(#my.library.name);
print(currentLibrary.simpleName);
// Find a declaration in the current library, and start from there.
var mainFunction = reflect(main) as ClosureMirror;
var alsoCurrentLibrary = mainFunction.function.owner as LibraryMirror;
print(alsoCurrentLibrary.simpleName);
}
What are you trying to do, which requires doing reflection?

How to structure a simple Dart web app

For Dart starters I'm working on a simple web which consists of less than 10 classes. I'm totally confused as for how to organized them in files, folders (and packages? and libraries?).
Currently I have
web/
img/
*.png
styles/
main.css
index.html
main.dart
*.dart
All but one Dart file contain a single class. Imports are done through import 'a.dart'; (e.g. in b.dart).
This is obviously wrong because the Dart Editor complains about
The imported libraries 'c.dart' and 'd.dart' should not have the same
name ''
I went through the respective sections in pub documentation and read about possible app structures in the Polymer docs. I also looked at the structure of the pop_pop_win sample application that comes with Dart. It's all a bit overwhelming because there are so many variations, options and combinations.
If I were you, I would create libraries and imports like so:
awesomeLibrary.dart
library awesome;
part 'foo.dart';
part 'bar.dart';
foo.dart
part of awesome;
class Foo
{
static function Baz() {}
}
bar.dart
part of awesome;
class Bar
{
...
}
main.dart
import 'awesomeLibrary.dart';
void main()
{
Foo.Baz(); // Imported library function
}

How to get a list of all loaded libraries in dart?

Is it possible to make some call from main to get all the loaded libraries at runtime?
main(){
Iterable<LibraryMirror> libraries = getAllLoadedLibraries();
}
I see there is a Type LibraryMirror in the mirrors package but I don't see how you would get a library mirror since you can't just reference them in code like a top level function/variable or class name.
import 'dart:mirrors`;
currentMirrorSystem().libraries;

How to reference another file in Dart?

I know you can use the library, import and even #import, but which is correct?
I have got two files, MainClass.dart and Library.Dart, and I want to add a reference to Library.dart in MainClass.dart. How can I do that?
Firstly let me just preface this by saying please do not use the hash symbol before import or library or anything else. This is an old syntax that is being deprecated. So we no longer want to use #import('...') The correct syntax is:
import 'some_file.dart';
That said, there are two different things we can do to access different dart source files within our current file. The first is to import the file. We use this such as in your case when you want to bring a different library into the current file (or more accurately current library).
Usually if your files are in the same directory, or a sub directory of the current one we would import them like this:
import 'lib/library.dart';
However If you are using the pub package layout you can also use some special short-cut references as well to import files (particularly from other packages you've imported). I highly suggest reading the documents on the pub site, as most applications and libraries are designed with this in mind. It also has suggestions on best naming conventions such as filenames in all lower case, and using underscore for spaces, and directory layouts.
The other important thing to know about bringing a dart file into another file, is that we can use the part and part of directives. This used to be called #source but was changed (with the removal of the hash sign) to reduce confusion. The part directive is used when we want to write a single library which spans multiple files. Say for instance you have an Awesome Library, which is starting to get a little large for a single file. We will create the main file of the library (not to be confused with the main method). This file will usually have the same name as the library itself.
// awesome_library.dart
library awesome_library;
import 'dart:math';
import '...';
// this injects all the content of secret_file.dart
// into this file right here almost as if it was
// here in the first place.
part 'src/secret_file.dart';
// The rest of our file here
// ...
The part directive basically takes everything from our src/secret_file.dart and inserts it into that part of the file. This allows us to split our huge Awesome Library into multiple smaller files that are easier to maintain. While not specifically required, it is helpful to use the part of directive in our secret_file.dart to help the editor know that it is "part of" the library.
// secret_file.dart
part of awesome_library;
// ... Rest of our secret_file code below.
Note that when using a part file like this, the part(s) (that is everything that is not the main file of the library) cannot import or use library declarations themselves. They import whatever is imported into the the main file, but they cannot add any additional imports.
For more information about library see this link.
Importing your own created libraries:
You will be importing the filename.dart and not the name of your library.
So if the name of your library is: myLib and it is saved in the file: someDartFile.dart you will have to
import 'someDartFile.dart';
If you have on Windows a library at: K:\SomeDir\someFile.dart you will need to write:
import '/K:/SomeDir/someFile.dart';
example:
import 'LibraryFile.dart'; //importing myLib
void main(){
//a class from myLib in the LibraryFile.dart file
var some = new SomeClassFromMyLibrary();
}
myLib in LibraryFile.dart:
library myLibrary;
import 'dart:math';
class SomeClassFromMyLibrary{
String _str = "this is some private String only to myLibrary";
String pubStr = "created instances of this class can access";
}
Here a full example.
//TestLib.dart
import 'LibFile.dart'; //SomeLibrary
void main() {
print("Hello, World!");
LibFile l = new LibFile();
print(l.publicString);//public
print(l.getPrivateString);//private
print(l.getMagicNumber); //42
}
//LibFile.dart
library SomeLibrary;
part 'LibFile2.dart';
class LibFile {
String _privateString = "private";
String publicString = "public";
String get getPrivateString => _privateString;
int get getMagicNumber => new LibFile2().number;
}
//LibFile2.dart
part of SomeLibrary;
class LibFile2 {
int number = 42;
}
Although i am answering very late, but the answer may help new developer.
Always use pubspec.yaml file in your dart package(application/library).
once you run pub get command it will add your local library in the dependencies list in .packages file.
Consider i have following project structure.
To refer to the content of greeting.dart in my main.dart file i should add the library as below
import 'package:my_project_name/greeting.dart'
Once imported we can use the content of greeting.dart file in our main.dart file.
Note: we have not used the actual path as you can see 'lib' directory is missing.
First make sure that's the name which you have mentioned in pubspec.yaml and the file you want to import are sharing the exact same name
example:
pubspec.yaml
name: flutter_wordpress_app
description: flutter wordpress app
...
....
// dirOne/dirTwo/greeting.dart
class FavArticleBloc {
// Your code goes here
}
import 'package:flutter_wordpress_app/dirOne/dirTwo/greeting.dart'
void main(){
var some = new FavArticleBloc();
}
But
in the main.dartyou don't need to specify
import 'package:flutter_wordpress_app
just do like below
import 'dirOne/dirTwo/greeting.dart

dart #import weirdness in dart editor

So I have two dart files -- One defines the entry-point Main() and the other is a class I've created. The Main file #imports dart:html and #sources my class. My class uses the dart:html namespace, and Dart Editor will display errors if I don't #import it. However, my class will fail to compile if I #import dart:html since the Main file already does, but compiles fine without the #import. Is there a way to appease the dart editor, or is this a known issue with how the dart editor resolves namespaces?
You should only do the import once and then source your program files from your main file. Something like this:
main.dart
#import("dart:html");
#source("program.dart");
main() {
var program = new Program();
program.run();
}
program.dart
class Program {
run() {
var elm = new Element.html("<p>hello world</p>");
document.body.nodes.add(elm);
}
}
should definitely work.

Resources