Page 34 - Dart Language
P. 34

Chapter 13: Libraries




        Remarks



        The import and library directives can help you create a modular and shareable code base. Every
        Dart app is a library, even if it doesn’t use a library directive. Libraries can be distributed using
        packages. See Pub Package and Asset Manager for information about pub, a package manager
        included in the SDK.


        Examples



        Using libraries


        Use import to specify how a namespace from one library is used in the scope of another library.


         import 'dart:html';


        The only required argument to import is a URI specifying the library. For built-in libraries, the URI
        has the special dart: scheme. For other libraries, you can use a file system path or the package:
        scheme. The package: scheme specifies libraries provided by a package manager such as the pub
        tool. For example:


         import 'dart:io';
         import 'package:mylib/mylib.dart';
         import 'package:utils/utils.dart';


        Libraries and visibility


        Unlike Java, Dart doesn’t have the keywords public, protected, and private. If an identifier starts
        with an underscore _, it’s private to its library.


        If you for example have class A in a separate library file (eg, other.dart), such as:


         library other;

         class A {
           int _private = 0;

           testA() {
             print('int value: $_private');  // 0
             _private = 5;
             print('int value: $_private'); // 5
           }
         }

        and then import it into your main app, such as:






        https://riptutorial.com/                                                                               29
   29   30   31   32   33   34   35   36   37   38   39