How to structure a simple Dart web app - dart

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
}

Related

what's the different between full path and relative path in dart

I develop a flutter app, define serveral models in 'model' package.
Then I declare a class Example in 'model' for example.
model/example.dart
class Example {
#override
String toString() {
return 'class example';
}
}
test_a.dart
import 'package:example/model/example.dart'
Example testA() {
return Example()
}
test.dart
import 'model/example.dart'
import 'test_a.dart'
test() {
Example example = testA();
if (example is Example) {
print('this class is Example');
} else {
print('$example');
}
}
I will get output class example🌚
If I change from import 'model/example.dart' to import 'package:example/model/example.dart' in test.dart, then I will get the output this class is Example.
So I'm confused what is different between the full path and relative path in dart.
package imports
'package:... imports work from everywhere to import files from lib/*.
relative imports
Relative imports are always relative to the importing file.
If lib/model/test.dart imports 'example.dart', it imports lib/model/example.dart.
If you want to import test/model_tests/fixture.dart from any file within test/*, you can only use relative imports because package imports always assume lib/.
This also applies for all other non-lib/ top-level directories like drive_test/, example/, tool/, ...
lib/main.dart
There is currently a known issue with entry-point files in lib/* like lib/main.dart in Flutter. https://github.com/dart-lang/sdk/issues/33076
Dart always assumed entry-point files to be in other top-level directories then lib/ (like bin/, web/, tool/, example/, ...).
Flutter broke this assumption.
Therefore you currently must not use relative imports in entry-point files inside lib/
See also
How to reference another file in Dart?

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 make a function/class from an imported library accessible for all the libraries imported below in Dart?

Suppose I have this:
benchmark.dart:
library benchmark;
benchmark() {...}
app.dart:
import 'benchmark.dart'; // functions from this lib are now accessible in this file
export 'benchmark.dart'; // does this make them accessible in all files imported below?
import 'model.dart';
void main() {
doSomething();
}
model.dart
doSomething() {
benchmark(); // => Error, no such method, unless
// I import 'benchmark.dart' above in this file!
}
Is this the right behavior? How do I make benchmark accessible without importing the lib in model.dart?
You have to make the imports in every library you want to use other libraries.
What you might do is to join several files to one library using part 'model.dart'; (parent file) and part of app; (linked file).
Then the types and functions imported in the parent file are available in all files of this library.

Dart library and parts are not bi-directionally resolved

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.

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

Resources