Importing packages in flutter using dart - flutter

for other packages like the flutter package we use the following import:
import 'package:flutter/material.dart';
but why are we doing this for dart packages?
import 'dart:io';
why can't it be same like flutter?:
import 'package:dart/io/platform.dart';

The package: prefix is used to import libraries found in packages while the dart: prefix is used to import the core libraries (such as io, collection, convert...).
Consequently, we don't use the package prefix for the core libraries because they're already built-in and no need to add them to the pubspec file.

Related

how to import local package files in my main package?

I want to import the about.dart file in my main.dart file. But it is showing error while I'm copying its path. I tried many ways to import it, but all fails. Can anyone please help me in this?
if both the files are in the same folder you can import them using this line
import './dialpad.dart';
if you want to import the file from a folder at the same level of the file in which you want to import you can use this line
import '../widgets/some_file.dart';
To use your local package you have to define package and give a package path inside your pubspec.yaml for the package.
To add local package in pubspec.yaml:
sip_up-0.5.6:
path: ./sip_up-0.5.6

Internationalization in a monorepo

I have an app (just frontend) whose code is structured in a monorepo. There are separate packages for each custom widget. I would also like to have one package with all the translations. In this way, if another package need a translation I just need to import that package. So I created the translation package and in its main file I just wrote (here I use intl):
export 'package:flutter_gen/gen_l10n/app_localizations.dart';
Now, if I import that package in another package and I import the file with:
import 'package:l10n/l10n.dart';
It tells me that that import is not used and gives me error when I use the translation with AppLocalizations.of(context)!.foo
If you use VScode (don't know for other editors) you have to manualy import the generated translation package:
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
It seems that it's not possible to export generated file by design (check out this github issue). The best solution that I've found to fix my problem is the following:
Create a translation package inside your project, like in packages/translations and add that package to the pubspec.yaml of your your main project. Like this:
dependencies:
flutter:
sdk: flutter
translations:
path: packages/translations
In that package create a l10n.yaml file like this:
arb-dir: lib
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
If you are working in a git repo, add packages/translations/lib/app_localizations*.dart in your .gitignore
Now if you run flutter packages get && flutter gen-l10n in that translations packages all the translations will be automatically generated (assuming they are in packages/translations/lib/app_xx.arb)
Now you can import the translation file from you main package with import 'package:translations/app_localizations.dart';
The only drawback is that the translations are not automatically generated when you type flutter pub get (see this issue). In order to regenerate them you have to type flutter gen-l10n inside the translations package every time. The solution could be improved by using a tool for managing Dart monorepo projects like melos.
An example implementation of what I described (including melos) could be found in this github repo

Flutter "import framework:dart" throws error

my Flutter dart script requires 'import framework:dart'. But Visual Studio Code's "Problems Tab" indicates this error:
Target of URI doesn't exist: 'framework:dart'.
Try creating the file referenced by the URI, or Try using a URI for a file that does exist.
Needing suggestions to remedy this.
Thanks!
If you use the flutter framework for dart, the import must be:
import 'package:flutter/material.dart';
if you want to use the Cupertino-style (==iOS theme) than you have to make following import:
import 'package:flutter/cupertino.dart';
If you have Flutter installed, this imports should work.
Because framework.dart is in the widgets.dart library packages, I think you need to import the widgets.dart library.
import 'package:flutter/widgets.dart';
For the usage, you can read the docs here.
I hope it will help.

VSCode dart code auto completion doesn't work properly for packages

I use VSCode for flutter development everything is good but autocompletion is not working properly for packages.I wonder what is wrong with my IDE settings
If I import packages without as keyword the code completion doesn't work but if I import them as something it works for example if I import like following :
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:http/http.dart'
The intellisense doesn't recognize FireStore and http but if I import like:
import 'package:cloud_firestore/cloud_firestore.dart' as fire;
import 'package:http/http.dart' as http;
now when I call fire It shows firestore class and methods and same is for http and every other package
Since you're not getting auto-suggestions, please check these guidelines
Please ensure, you have included your package in pubspec.yaml
Did you executed flutter pub get
Usually, we can use Ctrl+Space to get auto-suggestion on VSCode

I import another file by `import 'models.dart';`, it can't be compiled

I have defined many dart files in my project, and they imported another file by import:
controller.dart
import 'models.dart';
// dart code
app.dart
import 'models.dart';
import 'controller.dart';
// dart code
server.dart
import "app.dart";
main() {
//
}
more dart files
But when I run server.dart, it reports error:
a library which is imported is missing a library directive: models.dart
What does that mean? Do I have to declare them all as libraries?
From the language specification, it says:
It is a compile-time error if the compilation unit found at the
specified URI is not a library declaration.
It seems we can only import a library, not normal files.
But if I define 2 simple files,
a.dart
import "b.dart";
main() {
hello();
}
b.dart
hello() { print("hello!"); }
Then run a.dart:
dart a.dart
It can print hello!.
I get confused :(
Add library directive in each file that you want to import and all will be works fine.
In your case:
models.dart
library foo.models;
// dart code
controller.dart
library foo.controller;
import 'models.dart';
// dart code
app.dart
library foo.app;
import 'models.dart';
import 'controller.dart';
// dart code
server.dart
// If not planned to be imported then the name may be omitted
library foo.bin.server;
import "app.dart";
main() {
//
}
Remember that each library that will be imported by another library requires unique name.
The best way naming your libraries by prepending package name.
Eg.
The package name is "worker".
lib/worker.dart
library worker.worker
lib/work.dart
library worker.work