当前位置:网站首页>[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
2022-07-03 02:32:00 【Programmer community】
List of articles
- One 、FutureBuilder brief introduction
- Two 、FutureBuilder Construction method
- 3、 ... and 、AsyncSnapshot Asynchronous computation
- Four 、 Related resources
One 、FutureBuilder brief introduction
FutureBuilder take Asynchronous operations And asynchronous UI to update Bind together ; It can be Asynchronous operations Result , Asynchronous Update to UI In the interface ;
Asynchronous operation results : Network request , Database read , Wait for time-consuming operations The result ;
Two 、FutureBuilder Construction method
FutureBuilder The construction method is as follows :
/// Creates a widget that builds itself based on the latest snapshot of/// interaction with a [Future].////// The [builder] must not be null.const FutureBuilder({
Key? key, this.future, this.initialData, required this.builder,}) : assert(builder != null), super(key: key);FutureBuilder({
Key key, Future<T> future, T initialData, @required AsyncWidgetBuilder<T> builder })
FutureBuilder Parameter analysis of construction method :
- Future<T> future : Asynchronous computation related to asynchronous operations ;
/// The asynchronous computation to which this builder is currently connected, /// possibly null. /// /// If no future has yet completed, including in the case where [future] is /// null, the data provided to the [builder] will be set to [initialData]. final Future<T>? future;
- T initialData : Initialization data before asynchronous calculation ;
/// The data that will be used to create the snapshots provided until a /// non-null [future] has completed. /// /// If the future completes with an error, the data in the [AsyncSnapshot] /// provided to the [builder] will become null, regardless of [initialData]. /// (The error itself will be available in [AsyncSnapshot.error], and /// [AsyncSnapshot.hasError] will be true.) final T? initialData;
- @required AsyncWidgetBuilder<T> builder : AsyncWidgetBuilder Type of callback function , This is based on asynchronous interaction Widget Function of ;
/// Signature for strategies that build widgets based on asynchronous/// interaction.////// See also:////// * [StreamBuilder], which delegates to an [AsyncWidgetBuilder] to build/// itself based on a snapshot from interacting with a [Stream]./// * [FutureBuilder], which delegates to an [AsyncWidgetBuilder] to build/// itself based on a snapshot from interacting with a [Future].typedef AsyncWidgetBuilder<T> = Widget Function(BuildContext context, AsyncSnapshot<T> snapshot);
3、 ... and 、AsyncSnapshot Asynchronous computation
AsyncWidgetBuilder<T> The actual type of callback function is Widget Function(BuildContext context, AsyncSnapshot<T> snapshot) , Receive two parameters BuildContext context and AsyncSnapshot<T> snapshot , The return value is Widget Components ;
AsyncSnapshot<T> snapshot The parameter contains the information of asynchronous calculation ;
class AsyncSnapshot<T> {
/// Creates an [AsyncSnapshot] with the specified [connectionState], /// and optionally either [data] or [error] with an optional [stackTrace] /// (but not both data and error). const AsyncSnapshot._(this.connectionState, this.data, this.error, this.stackTrace) : assert(connectionState != null), assert(!(data != null && error != null)), assert(stackTrace == null || error != null); /// Current state of connection to the asynchronous computation. final ConnectionState connectionState; /// The latest data received by the asynchronous computation. /// /// If this is non-null, [hasData] will be true. /// /// If [error] is not null, this will be null. See [hasError]. /// /// If the asynchronous computation has never returned a value, this may be /// set to an initial data value specified by the relevant widget. See /// [FutureBuilder.initialData] and [StreamBuilder.initialData]. final T? data; /// The latest error object received by the asynchronous computation. /// /// If this is non-null, [hasError] will be true. /// /// If [data] is not null, this will be null. final Object? error; /// Returns whether this snapshot contains a non-null [data] value. /// /// This can be false even when the asynchronous computation has completed /// successfully, if the computation did not return a non-null value. For /// example, a [Future<void>] will complete with the null value even if it /// completes successfully. bool get hasData => data != null; /// Returns whether this snapshot contains a non-null [error] value. /// /// This is always true if the asynchronous computation's last result was /// failure. bool get hasError => error != null;}
AsyncSnapshot<T> snapshot Medium ConnectionState connectionState It's the connection state , Is an enumeration value , There are four values :
- none
- waiting
- active
- done
/// The state of connection to an asynchronous computation.////// The usual flow of state is as follows:////// 1. [none], maybe with some initial data./// 2. [waiting], indicating that the asynchronous operation has begun,/// typically with the data being null./// 3. [active], with data being non-null, and possible changing over time./// 4. [done], with data being non-null.////// See also:////// * [AsyncSnapshot], which augments a connection state with information/// received from the asynchronous computation.enum ConnectionState {
/// Not currently connected to any asynchronous computation. /// /// For example, a [FutureBuilder] whose [FutureBuilder.future] is null. none, /// Connected to an asynchronous computation and awaiting interaction. waiting, /// Connected to an active asynchronous computation. /// /// For example, a [Stream] that has returned at least one value, but is not /// yet done. active, /// Connected to a terminated asynchronous computation. done,}
final T? data It is the latest data received by asynchronous calculation ;
Object? error Is the error object received by asynchronous calculation ;
AsyncSnapshot<T> snapshot There is also hasData and hasError Two attributes , hasData Used to check whether the object contains non empty data values , hasError Used to check whether the error value is included ;
/// Returns whether this snapshot contains a non-null [data] value. /// /// This can be false even when the asynchronous computation has completed /// successfully, if the computation did not return a non-null value. For /// example, a [Future<void>] will complete with the null value even if it /// completes successfully. bool get hasData => data != null; /// Returns whether this snapshot contains a non-null [error] value. /// /// This is always true if the asynchronous computation's last result was /// failure. bool get hasError => error != null;
Four 、 Related resources
Reference material :
- Flutter Official website : https://flutter.dev/
- Flutter Plug in download address : https://pub.dev/packages
- Flutter Developing documents : https://flutter.cn/docs ( Strongly recommend )
- official GitHub Address : https://github.com/flutter
- Flutter The Chinese community : https://flutter.cn/
- Flutter Practical tutorial : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart Chinese document : https://dart.cn/
- Dart Developer website : https://api.dart.dev/
- Flutter Chinese net : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter Related issues : https://flutterchina.club/faq/ ( It is recommended to watch it at the introductory stage )
- GitHub Upper Flutter Open source examples : https://download.csdn.net/download/han1202012/15989510
- Flutter Practical e-books : https://book.flutterchina.club/chapter1/
- Dart Language practice website : https://dartpad.dartlang.org/
Important topics :
- Flutter Animation reference documentation : https://flutterchina.club/animations/
Blog source download :
GitHub Address : https://github.com/han1202012/flutter_http( Keep updating with the progress of the blog , There may not be the source code of this blog )
Blog source snapshot : https://download.csdn.net/download/han1202012/21528472 ( The source code snapshot of this blog , You can find the source code of this blog )
边栏推荐
猜你喜欢
Build a private cloud disk cloudrev
Producer consumer model based on thread pool (including blocking queue)
How to change the panet layer in yolov5 to bifpn
[translation] the background project has joined the CNCF incubator
PyTorch 卷积网络正则化 DropBlock
Pytorch convolution network regularization dropblock
Random shuffle note
What does "where 1=1" mean
Distributed transaction solution
Servlet中数据传到JSP页面使用el表达式${}无法显示问题
随机推荐
【翻译】具有集中控制平面的现代应用负载平衡
Use go language to realize try{}catch{}finally
基于can总线的A2L文件解析(2)
5.文件操作
【翻译】Flux安全。通过模糊处理获得更多信心
Distributed transaction solution
Gbase 8C system table PG_ cast
内存池(内核角度理解new开辟空间的过程)
2022-2028 global splicing display industry research and trend analysis report
Apple releases MacOS 11.6.4 update: mainly security fixes
Packing and unpacking of JS
Gbase 8C system table PG_ amproc
GBase 8c系统表-pg_am
Gbase 8C function / stored procedure parameters (I)
面试八股文整理版
GBase 8c系统表-pg_constraint
SPI机制
cvpr2022去雨去雾
Awk from getting started to getting into the ground (3) the built-in functions printf and print of awk realize formatted printing
Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque