Dart是最初由 Google 开发的开源编程语言。它适用于服务器端和用户端。DartSDK带有其编译器-DartVM和这意味着生成JavaScript相当于一个Dart脚本,以便它可以在这些网站也不支持Dart运行的程序dart2js。
Dart是一种面向对象的语言,与Java Programming非常相似。 Dart被广泛用于创建单页网站和网络应用程序。 dart应用程序的最佳示例是Gmail 。
在本文中,我们将研究dart库以及使用它们的过程。
导入库
要在dart使用库,我们必须先导入它们。导入使库的组件在当前文件中可用。 Dart有一个特殊的关键字import来导入库。
Syntax: import 'dart:sync'
Dart
// Example showing importing a library
// Importing built-in library
import 'dart:math';
void main() {
print("Square root of 25 is: ${sqrt(25)}");
}
Dart
// Example illustrating creation of a custom library
library basic_calc;
import 'dart:math';
// library content
int add(int a,int b){
print("Add Method ") ;
return a+b;
}
int multiplication(int a,int b){
print("Multiplication Method") ;
return a*b;
}
int subtraction(int a,int b){
print("Subtraction Method") ;
return a-b;
}
int modulus(int a,int b){
print("Modulus Method") ;
return a%b;
}
Dart
import 'basic.dart';
void main() {
var a = 50;
var b = 30;
var sum = add(n1,n2);
var mod = modulus(n1,n2);
var mul = multiplication(n1,n2);
var div = divide(n1,n2);
var sub = subtraction(n1,n2);
print("$n1 + $n2 = $sum");
print("$n1 % $n2= $mod");
print("$n1 + $n2 = $mul");
print("$n1 - $n2 = $sub");
}
Dart
// Example showing encapsulation in dart
// Define a library naming cake
library cake;
class MainCake{
// non-private property
// list of strings
List randomPieceOfCakes = ['chocolate',
'butterscotch',
'vanilla',
'strawberry'];
// private properties
String _pieceOfCake1 = "chocolate";
String pieceOfCake2 = "butterscotch";
}
Dart
import 'cake.dart';
void main(){
MainCake newCake = new MainCake();
// non-private property - randomPieceOfCakes
print(newCake.randomPieceOfCakes);
// private property - piece of cake
// private property error
print(newCake._pieceOfCake1);
// non-private private - piece of cake
print(newCake.pieceOfCake2);
}
Dart
// Example showing the use of as keyword
// Defining a lib names greetings
library greetings;
void sayHi(msg){
print("Hello coder. Welcome to ${msg}");
}
Dart
library hellogreetings;
void sayHi(msg){
print("${msg} has solutions of all your problems");
}
Dart
import 'greetings.dart';
import 'hellogreetings.dart' as gret;
// using as prefix avoids function name clashes
void main(){
sayHi("GFG");
// To eliminate the name confliction
gret.sayHi("GFG");
}
输出:
Square root of 25 is: 5
当从另一个包导入库文件时,“package: directive”用于指定该文件的 URI:
import 'package:utilities/utilities.dart';
创建自定义库
自定义库是用户根据需要创建的库。此类库称为用户定义库。 Dart支持创建此类库,我们可以在需要时导入它们。这主要分两步进行:
1. 声明
必须使用关键字“library”声明库名称
library ‘my_lib’ //any random lib name
2. 连接
库可以在同一目录中或从另一个目录中连接。
//within same directory
import ‘library_name’
//from another directory
import ‘dir/library_name’
Dart
// Example illustrating creation of a custom library
library basic_calc;
import 'dart:math';
// library content
int add(int a,int b){
print("Add Method ") ;
return a+b;
}
int multiplication(int a,int b){
print("Multiplication Method") ;
return a*b;
}
int subtraction(int a,int b){
print("Subtraction Method") ;
return a-b;
}
int modulus(int a,int b){
print("Modulus Method") ;
return a%b;
}
我们创建了一个名为“ basic_calc ”的自定义库。现在我们必须在当前文件中导入我们的库:
Dart
import 'basic.dart';
void main() {
var a = 50;
var b = 30;
var sum = add(n1,n2);
var mod = modulus(n1,n2);
var mul = multiplication(n1,n2);
var div = divide(n1,n2);
var sub = subtraction(n1,n2);
print("$n1 + $n2 = $sum");
print("$n1 % $n2= $mod");
print("$n1 + $n2 = $mul");
print("$n1 - $n2 = $sub");
}
输出:
Add Method
Multiplication Method
Subtraction Method
Modulus Method
50 + 30 = 80
50 % 30= 20
50 * 30 = 1500
50 - 30 = 20
封装
dart为其用户提供的另一个功能是库的封装。封装用于将数据和函数组合成一个称为类的单元。我们可以通过使用_(下划线),后跟标识符来实现在dart的封装。 _(下划线)符号用于使库的内容完全私有。
注意:与其他 OOP 语言不同, Dart的封装发生在库级别而不是类级别。
Syntax: _identifier
例子:
Dart
// Example showing encapsulation in dart
// Define a library naming cake
library cake;
class MainCake{
// non-private property
// list of strings
List randomPieceOfCakes = ['chocolate',
'butterscotch',
'vanilla',
'strawberry'];
// private properties
String _pieceOfCake1 = "chocolate";
String pieceOfCake2 = "butterscotch";
}
在这里,我们将导入我们上面创建的蛋糕库:
Dart
import 'cake.dart';
void main(){
MainCake newCake = new MainCake();
// non-private property - randomPieceOfCakes
print(newCake.randomPieceOfCakes);
// private property - piece of cake
// private property error
print(newCake._pieceOfCake1);
// non-private private - piece of cake
print(newCake.pieceOfCake2);
}
作为关键字
我们可以在单个当前文件中导入多个库。但是如果我们创建两个或多个同名的函数,编译器将无法区分两者并提供错误的输出。为了避免这种情况, dart为我们提供了关键字“as”来命名库的别名。
Syntax: import 'my_lib' as prefix
例子:
Dart
// Example showing the use of as keyword
// Defining a lib names greetings
library greetings;
void sayHi(msg){
print("Hello coder. Welcome to ${msg}");
}
现在我们将定义另一个库
Dart
library hellogreetings;
void sayHi(msg){
print("${msg} has solutions of all your problems");
}
现在,我们以它们为前缀导入上述库
Dart
import 'greetings.dart';
import 'hellogreetings.dart' as gret;
// using as prefix avoids function name clashes
void main(){
sayHi("GFG");
// To eliminate the name confliction
gret.sayHi("GFG");
}
输出:
Hello coder. Welcome to GFG
GFG has solutions of all your problems
以下是Dart的核心库:
TYPE | LIBRARY | DESCRIPTION |
---|---|---|
Multi-Platform Libraries | dart:async | This library supports asynchronous programming, with classes such as Future and Stream. |
dart:collection | This library contains classes and utilities that supplement the collection support in dart:core. | |
dart:convert | This library contains encoders and decoders for converting between different data representations, including JSON and UTF-8. | |
dart:core | This library contains built-in types, collections, and other core functionality for every Dart program. | |
dart:developer | This library provides interaction with developer tools such as the debugger and inspector. | |
dart:math | This library contains mathematical constants and functions, plus a random number generator. | |
dart:typed_data | This library contains lists that efficiently handle fixed sized data files(for example, unsigned 8-byte integers) and SIMD numeric types. | |
Native platform libraries | dart:io | This library contains file, sockets, HTTP, and other I/O support for non-web applications. |
dart:isolate | This library supports Concurrent programming using isolates independent workers similar to threads. | |
Web platform libraries | dart:html | This library contains HTML elements and other resources for web-based applications. |
dart:indexed_db | This library supports a Client-side key-value store with support for indexes. | |
dart:web_audio | This library supports High-fidelity audio programming in the browser. | |
dart:web_gl | This library supports 3D programming in the browser. |