Using rascal from the command line, when I type:
import demo::hello
I receive a ModuleNameMismatch error, although the current directory (the one in which I type java -jar rascal-shell-stable.jar) contains a subdirectory named demo containing a file hello.rsc :
module hello
import IO;
void hello(){
println("Hello word");
}
It however works when I type
import hello
and the hello.rsc is in the current directory.
For your info: I am on Windows 10.
The module name must be the full relative path to the module.rsc file.
In this case, the hello.rsc file should be :
module demo::hello
import IO;
void hello(){
println("Hello word");
}
That the reason why it does work when the hello.rsc in the current directory.
Related
ERROR in node_modules/ng2-toastr/src/toast-container.component.d.ts(1,48): error TS2305: Module '"D:/angular basic/my-dream-app/node_modules/#angular/core/core"' has no exported member 'AnimationTransitionEvent'.
using ng2-toastr {ToastsManager} in Angular6
Follow this URL it will get resolved.
It has proper steps as below:
Change ng2-toastr to ngx-toastr and set the version to 8.10.2.
Run npm install to download the package
Assuming you were importing ng2-toastr.min.css somewhere in your app.scss or angular.json, import this instead: #import '~ngx-toastr/toastr.css';
Replace ToastsManager with ToastrService, imported from ngx-toastr:
import { ToastrService } from 'ngx-toastr';
Replace ToastModule with ToastrModule (most likely you are importing this in your app.module.ts file:
import { ToastrModule } from 'ngx-toastr';
If you were calling toastr.setRootViewContainerRef anywhere in your code, that can be deleted since it's no longer needed.
AnimationTransitionEvent has been renamed to AnimationEvent. Please import it from #angular/animations.
According to this tutorial, when defining routes in Angular for Dart you need to import the template.dart files of the relevant components:
import 'crisis_list_component.template.dart' as clct;
import 'hero_list_component.template.dart' as hlct;
I have put my components in a subfolder of the folder where this code resides. I have the import code in lib/src/routes.dart:
import 'components/foobar/foobar_component.template.dart' as fct;
and a Foobar component in lib/src/components/foobar/foorbar_component.dart.
When I have the foobar_component.dart code in src (i.e. the same folder as routes.dart) there's no problem. But when I move it to its own subfolder, I get the error:
Unable to find modules for some sources [...] Please check the following imports: import 'components/foobar/foobar_component.template.dart'
How do I get it to find the component in its subfolder?
You could use
import '../../components/foobar/foobar_component.template.dart' as fct;
but usually, it is better to not use ../ in imports and instead use package imports
import 'package:my_package/web/src/components/foobar/foobar_component.template.dart' as fct;
where my_package is the string used in name: ... in your pubspec.yaml and lib/ is skipped in the path to the imported file.
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 do I read the name of a File or a Directory?
There is a property 'path' but that returns the entire file path.
Would be nice to have a property like 'name' that just returns the last part of the path.
In Java there is a method called File.name();
You can use the path package to do that :
import 'package:path/path.dart' as path;
main() {
path.basename('path/to/foo.dart'); // -> 'foo.dart'
path.basename('path/to'); // -> 'to'
}
See the path package documentation for more explanations.
Since Dart Version 2.6 has been announced and it's available for flutter version 1.12 and higher, You can use extension methods. It will provide a more readable and global solution to this problem.
file_extensions.dart :
import 'dart:io';
extension FileExtention on FileSystemEntity{
String get name {
return this?.path?.split("/")?.last;
}
}
and name getter is added to all the file objects. You can simply just call name on any file.
main() {
File file = new File("/dev/dart/work/hello/app.dart");
print(file.name);
}
Read the document for more information.
Note:
Since extension is a new feature, it's not fully integrated into IDEs yet and it may not be recognized automatically. You have to import your extension manually wherever you need that. Just make sure the extension file is imported:
import 'package:<your_extention_path>/file_extentions.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