How to use the import keyword in Dart
Overview
In Dart, we use the import keyword to import libraries or other files within a program.
Syntax
import library_name;
Assume we need some mathematical constant such as pi or a function like sqrt() in our program, then we have to import the dart: math.
Code
The following code shows how to use the import keyword in Dart.
// import keyword in dartimport 'dart:math';void main() {var radius = 4;var cir = 2 *pi * radius;print("The circumference of a circle: $cir");// using sqrt() to find square root of 81print("The square root of 81 is: ${sqrt(81)}");}
Explanation
- Line 2: We import the
dart: mathlibrary to access thepiand thesqrt()function. - Line 3: We create a
main()function. - Line 4: We declare a variable named
radiusand assign a value to it. - Line 5: We declare a variable named
cirwhich holds the value from the computation. - Line 6: We display the result using
print(). - Line 8: We calculate the square root of 81 using the
sqrt()function from thedart:mathlibrary. Finally, we display the result.