Dart custom libraries, system libraries and third party libraries

/*

Basically previously described the basics all the time Dart Dart write code in a file inside, but the actual development can not be so written, modular very important, so this requires the use of the concept of the library.

In the Dart, import keywords introduced by the use of the library.

library instruction can create a library, each file is a library Dart, even without the use of library instruction to specify.


Dart in the library there are three:

    1, our custom library     
          import 'lib/xxx.dart';
    2, the system built-in library       
          import 'dart:math';    
          import 'dart:io'; 
          import 'dart:convert';
    3, Pub package management system in the library  
        https://pub.dev/packages
        https://pub.flutter-io.cn/packages
        https://pub.dartlang.org/flutter/

        1, it is necessary they want to create a new project root directory pubspec.yaml
        2, the configuration file and pubspec.yaml name, description, and other information dependent
        3, and then run the pub get get package downloaded to the local  
        4, import library project import 'package: http / http.dart' as http; see document uses

*/

Import local library

import 'lib/Animal.dart';
main(){
  var A = new new Animal ( 'small black dog', 20 );
  
  print(a.getName());
}
class Animal {
   String the _name;    // private attributes 
  int Age; 
   // default constructor shorthand 
  Animal (. this._name, the this Age);

  void printInfo(){   
    print("${this._name}----${this.age}");
  }

  String getName(){ 
    return this._name;
  } 
  void _run(){
    Print ( "It is a private method" );
  }

  execRun(){
    the this ._run ();   // call each other inside the class method 
  }
}

Built into the system library

// import 'dart:io';
import "dart:math";
main(){
 
    print ( min (12.23 ));

    print(max(12,25));
    
}
import 'dart:io';
import 'dart:convert';


void main() async{
  var result = await getDataFromZhihuAPI();
  print(result);
}


// API interfaces: http://news-at.zhihu.com/api/3/stories/latest 
getDataFromZhihuAPI (the async {)
   // . 1, the object create HttpClient 
  var httpClient = new new HttpClient ();  
   // 2, creates Uri Object 
  var URI = new new Uri.http ( 'news-at.zhihu.com', '/ API /. 3 / Stories / Latest' );
  
  // 3, initiating a request, wait request 
  var Request = the await httpClient. GetUrl (URI);
   // . 4, close request, waits for a response 
  var Response = the await Request. Close ();
   // . 5, the decoded content in response to 
  return the await Response . .transform (utf8.decoder) the Join ();
}

Description:

/*
async和await
  These two keywords only need to remember two things:
    Only async method can call the method using the await keyword
    If you call another async method must await keyword


async method is to become asynchronous.
await is waiting for the asynchronous method execution is completed.


*/

void main() async{
  var result = await testAsync();
  print(result);

}

// asynchronous method 
testAsync () the async {
   return 'the async the Hello' ;
}

Import third-party libraries

/*
pub package management system:


1, find the use of the library from the following URL
        https://pub.dev/packages
        https://pub.flutter-io.cn/packages
        https://pub.dartlang.org/flutter/

2, create a pubspec.yaml file, as follows

    name: xxx
    description: A new flutter module project.
    dependencies:  
        http: ^0.12.0+2
        date_format: ^1.0.6

3, configuration dependencies

4, run pub get get the remote repository

5, see the introduction of a document library
*/
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'package:date_format/date_format.dart';

main() async {
  // var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";

  //   // Await the http get response, then decode the json-formatted responce.
  //   var response = await http.get(url);
  //   if (response.statusCode == 200) {
  //     var jsonResponse = convert.jsonDecode(response.body);
     
  //     print(jsonResponse);
  //   } else {
  //     print("Request failed with status: ${response.statusCode}.");
  //   }


  
    print(formatDate(DateTime(1989, 2, 21), [yyyy, '*', mm, '*', dd]));

}

Heavy Dart Dart library naming conflict resolution

/*
1, conflict resolution
When introduced two library has the same name identifier, if it is java we usually specify the specific identifiers used by writing on the full path name of the package, even without import can be, but there is a need to import the Dart. When the conflict can be used as a prefix to specify the keyword library. As shown in the following example:

    import 'package:lib1/lib1.dart';
    import 'package:lib2/lib2.dart' as lib2;


    Element element1 = new Element();           // Uses Element from lib1.
    lib2.Element element2 = new lib2.Element(); // Uses Element from lib2.

*/

import 'lib/Person1.dart';
import 'lib/Person2.dart' as lib;


main(List<String> args) {
  P1 the Person = new new the Person ( 'John Doe', 20 );
  p1.printInfo();


  lib .Person P2 = new new lib.Person ( 'John Doe', 20 );
  p2.printInfo();

}

Part to import:

/*
Partial import
  If you only need to import part of the library, there are two modes:
     Mode 1: introduction portion need only use keywords show, the following example shows:
      import 'package:lib1/lib1.dart' show foo;
     Mode 2: hide unnecessary portions, hide using keywords, the following example shows:
      import 'package:lib2/lib2.dart' hide foo;      
*/

// import 'lib/myMath.dart' show getAge;

 import 'lib/myMath.dart' ;

void main(){
  getName();
  
  
}

Lazy loading

/*
Lazy loading

    Also known as lazy loading, can then be loaded when needed.
    The greatest benefit is the reduction of lazy loading APP startup time.

    Lazy loading using deferred as specified keyword, the following example shows:

    import 'package:deferred/hello.dart' deferred as hello;

    When you want to use, you need to use loadLibrary () method to load:

    greet() async {
      await hello.loadLibrary();
      hello.printGreeting();
    }


*/

 

Guess you like

Origin www.cnblogs.com/loaderman/p/11027076.html