Stories

Detail Return Return

Dart學習筆記:API - Stories Detail

本文更新於2024-12-25,使用Dart 2.18.2。

目錄
  • dart:async 【異步庫】
      • Future<T> —— 異步計算結果
      • FutureOr<T> —— 同步或異步的計算結果
      • Stream<T> —— 流
  • dart:convert 【數據轉換庫】
      • JsonCodec —— JSON編解碼器
      • Utf8Codec —— UTF-8編解碼器
    • 常量
      • json —— JSON編解碼
      • utf8 —— UTF-8編解碼
  • dart:core 【核心庫】
      • DateTime —— 日期時間
      • double —— 浮點數
      • Duration —— 時長
      • int —— 整數
      • List<E> —— 列表
      • Map<K, V> —— 映射
      • Object —— 對象
      • Set<E> —— 集合
      • Uri —— URI地址
  • dart:developer 【開發庫】
      • Timeline —— 時間線
  • dart:io 【I/O庫】
      • HttpClient —— HTTP客户端
      • HttpClientRequest —— HTTP客户端請求
      • HttpClientResponse —— HTTP客户端響應
      • HttpHeaders —— HTTP頭
    • 函數
      • sleep —— 睡眠
  • dart:math 【數學庫】
      • Random —— 隨機數
  • dart:mirrors 【反射庫】
      • LibraryDependencyMirror —— 庫依賴反射

官方文檔:https://api.dart.dev或https://api.dart.cn

dart:async 【異步庫】

import 'dart:async';

Future<T> —— 異步計算結果

  • 構造函數

    Future(FutureOr<T> computation())
    

    延遲計算:

    Future.delayed(Duration duration, [FutureOr<T> computation()?])
    
  • 方法

    • then<R>(FutureOr<R> onValue(T value), {Function? onError}) → Future<R>:註冊計算完成後的回調。

FutureOr<T> —— 同步或異步的計算結果

可表示Future<T>T類型。

Stream<T> —— 流

  • 方法
    • join([String separator = ""]) → Future<String>:將流內容連接成字符串。

dart:convert 【數據轉換庫】

import 'dart:convert';

JsonCodec —— JSON編解碼器

  • 方法
    • decode(String source, {Object? reviver(Object? key, Object? value)?}) → dynamic:解碼。
    • encode(Object? value, {Object? toEncodable(dynamic object)?}) → String:編碼。

Utf8Codec —— UTF-8編解碼器

  • 屬性
    • decoder → Utf8Decoder:解碼器。
    • encoder → Utf8Encoder:編碼器。

常量

json —— JSON編解碼

utf8 —— UTF-8編解碼

dart:core 【核心庫】

自動導入。

DateTime —— 日期時間

  • 構造函數

    當前日期時間:

    DateTime.now()
    
  • 屬性

    • day → int:日。
    • hour → int:時。
    • minute → int:分。
    • month → int:月。
    • second → int:秒。
    • year → int:年。
  • 方法

    • add(Duration duration) → DateTime:加。
    • subtract(Duration duration) → DateTime:減。

double —— 浮點數

64位。

  • 屬性
    • isInfinite → bool:是否是正負無窮大。

Duration —— 時長

  • 構造函數
    Duration({int days = 0, int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0, int microseconds = 0})
    

int —— 整數

64位。

  • 方法
    • toString() → String:轉換成字符串。
  • 靜態方法
    • parse(String source, {int? radix}) → int:從字符串解析整數。

List<E> —— 列表

  • 構造函數

    創建包含若干個元素的列表:

    List.generate(int length, E generator(int index), {bool growable = true})
    
  • 屬性

    • length ↔ int:長度。
  • 方法

    • map<T>(T toElement(E e)) → Iterable<T>:對每個元素映射出新的元素。
  • 運算符

    • operator +(List<E> other) → List<E>:加。和另一個列表連接成一個新列表。

Map<K, V> —— 映射

  • 構造函數
    Map()
    
  • 屬性
    • length → int:長度。
  • 方法
    • containsKey(Object? key) → bool:是否包含鍵。
    • remove(Object? key) → V?:刪除鍵。

Object —— 對象

  • 屬性
    • runtimeType → Type:運行時的對象類型。

Set<E> —— 集合

  • 屬性
    • length → int:長度。
  • 方法
    • add(E value) → bool:添加元素。
    • addAll(Iterable<E> elements) → void:添加所有多個元素。
    • clear() → void:清空所有元素。
    • contains(Object? value) → bool:是否包含元素。
    • containsAll(Iterable<Object?> other) → bool:是否包含所有多個元素。
    • remove(Object? value) → bool:刪除元素。
    • removeAll(Iterable<Object?> elements) → void:刪除所有多個元素。

Uri —— URI地址

  • 靜態方法
    • parse(String uri, [int start = 0, int? end]) → Uri:從字符串解析。

dart:developer 【開發庫】

import 'dart:developer';

Timeline —— 時間線

  • 靜態方法
    • finishSync() → void:結束同步。
    • startSync(String name, {Map? arguments, Flow? flow}) → void:開始同步。

dart:io 【I/O庫】

import 'dart:io';

HttpClient —— HTTP客户端

  • 構造函數
    HttpClient({SecurityContext? context})
    
  • 方法
    • close({bool force = false}) → void:關閉。
    • getUrl(Uri url) → Future:以URL發送GET請求。

HttpClientRequest —— HTTP客户端請求

  • 屬性
    • headers → HttpHeaders:請求頭。
  • 方法
    • close() → Future<HttpClientResponse>:關閉請求,返回響應。

HttpClientResponse —— HTTP客户端響應

  • 方法
    • transform<S>(StreamTransformer<List<int>, S> streamTransformer) → Stream<S>:轉換流。

HttpHeaders —— HTTP頭

  • add(String name, Object value, {bool preserveHeaderCase = false}) → void:添加。

函數

sleep —— 睡眠

void sleep( Duration duration )

dart:math 【數學庫】

import 'dart:math';

Random —— 隨機數

  • 構造函數
    偽隨機數:

    Random([int? seed])
    

    安全的隨機數:

    Random.secure()
    
  • 方法

    • nextInt(int max) → int:返回[0, max)區間的隨機整數。

dart:mirrors 【反射庫】

import 'dart:mirrors';

LibraryDependencyMirror —— 庫依賴反射

  • 方法
    • loadLibrary() → Future<LibraryMirror>:加載庫。
user avatar hubert-style Avatar HarmonyOS5 Avatar hhsk Avatar mybj123 Avatar
Favorites 4 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.