a library in a programming language represents a collection of routines (set of programming instructions). dart has a set of built-in libraries that are useful to store routines that are frequently used. a dart library comprises of a set of classes, constants, functions, typedefs, properties, and exceptions.
importing a library
importing makes the components in a library available to the caller code. the import keyword is used to achieve the same. a dart file can have multiple import statements.
built in dart library uris use the dart: scheme to refer to a library. other libraries can use a file system path or the package: scheme to specify its uri. libraries provided by a package manager such as the pub tool uses the package: scheme.
the syntax for importing a library in dart is given below −
import 'uri'
consider the following code snippet −
import 'dart:io' import 'package:lib1/libfile.dart'
if you want to use only part of a library, you can selectively import the library. the syntax for the same is given below −
import 'package: lib1/lib1.dart' show foo, bar; // import only foo and bar. import 'package: mylib/mylib.dart' hide foo; // import all names except foo
some commonly used libraries are given below −
sr.no | library & description |
---|---|
1 |
dart:io file, socket, http, and other i/o support for server applications. this library does not work in browser-based applications. this library is imported by default. |
2 |
dart:core built-in types, collections, and other core functionality for every dart program. this library is automatically imported. |
3 |
dart: math mathematical constants and functions, plus a random number generator. |
4 |
dart: convert encoders and decoders for converting between different data representations, including json and utf-8. |
5 |
dart: typed_data lists that efficiently handle fixed sized data (for example, unsigned 8 byte integers). |
example : importing and using a library
the following example imports the built-in library dart: math. the snippet calls the sqrt() function from the math library. this function returns the square root of a number passed to it.
import 'dart:math'; void main() { print("square root of 36 is: ${sqrt(36)}"); }
output
square root of 36 is: 6.0
encapsulation in libraries
dart scripts can prefix identifiers with an underscore ( _ ) to mark its components private. simply put, dart libraries can restrict access to its content by external scripts. this is termed as encapsulation. the syntax for the same is given below −
syntax
_identifier
example
at first, define a library with a private function.
library loggerlib; void _log(msg) { print("log method called in loggerlib msg:$msg"); }
next, import the library
import 'test.dart' as web; void main() { web._log("hello from webloggerlib"); }
the above code will result in an error.
unhandled exception: no top-level method 'web._log' declared. nosuchmethoderror: method not found: 'web._log' receiver: top-level arguments: [...] #0 nosuchmethoderror._thrownew (dart:core-patch/errors_patch.dart:184) #1 main (file:///c:/users/administrator/webstormprojects/untitled/assertion.dart:6:3) #2 _startisolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261) #3 _rawreceiveportimpl._handlemessage (dart:isolate-patch/isolate_patch.dart:148)
creating custom libraries
dart also allows you to use your own code as a library. creating a custom library involves the following steps −
step 1: declaring a library
to explicitly declare a library, use the library statement. the syntax for declaring a library is as given below −
library library_name // library contents go here
step 2: associating a library
you can associate a library in two ways −
- within the same directory
import 'library_name'
- from a different directory
import 'dir/library_name'
example: custom library
first, let us define a custom library, calculator.dart.
library calculator_lib; import 'dart:math'; //import statement after the libaray statement int add(int firstnumber,int secondnumber){ print("inside add method of calculator library ") ; return firstnumber+secondnumber; } int modulus(int firstnumber,int secondnumber){ print("inside modulus method of calculator library ") ; return firstnumber%secondnumber; } int random(int no){ return new random().nextint(no); }
next, we will import the library −
import 'calculator.dart'; void main() { var num1 = 10; var num2 = 20; var sum = add(num1,num2); var mod = modulus(num1,num2); var r = random(10); print("$num1 + $num2 = $sum"); print("$num1 % $num2= $mod"); print("random no $r"); }
the program should produce the following output −
inside add method of calculator library inside modulus method of calculator library 10 + 20 = 30 10 % 20= 10 random no 0
library prefix
if you import two libraries with conflicting identifiers, then you can specify a prefix for one or both libraries. use the 'as' keyword for specifying the prefix. the syntax for the same is given below −
syntax
import 'library_uri' as prefix
example
first, let us define a library: loggerlib.dart.
library loggerlib; void log(msg){ print("log method called in loggerlib msg:$msg"); }
next, we will define another library: webloggerlib.dart.
library webloggerlib; void log(msg){ print("log method called in webloggerlib msg:$msg"); }
finally, we will import the library with a prefix.
import 'loggerlib.dart'; import 'webloggerlib.dart' as web; // prefix avoids function name clashes void main(){ log("hello from loggerlib"); web.log("hello from webloggerlib"); }
it will produce the following output −
log method called in loggerlib msg:hello from loggerlib log method called in webloggerlib msg:hello from webloggerlib