flutter用 FutureBuilder 提高开发效率

常见场景

  1. 展示请求按钮
  2. 用户点击按钮,显示loading
  3. 展示数据或者错误

16bb15a10a125482.gif

抽象模式

  1. 展示请求按钮(初始状态
  2. 用户点击按钮,显示loading(请求中状态
  3. 展示数据或者错误 (结束状态(成功或失败)

转换成程序语言

以上三种现实情况对应 AsyncSnapshot 三个状态

  • ConnectionState.none 初始态
  • ConnectionState.waiting 请求态
  • ConnectionState.done 完成态
    • snapshot.hasError 完成(异常)
    • snapshot.hasData 完成(正常)

使用 FutureBuilder 处理这个场景

这篇文章的主角,FutureBuilder 就是为了解决这个问题存在的。它接收一个 Future 请求,和对应以上几种情况的 widget 回调。即可把数据和界面串联起来,避免额外声明仅用来传递数据用的变量

提前声明了一个 _showResult 变量,以表示页面是否触发请求。 并且封装了一个 _fetch() 网络请求。

Future<Map> _fetch() async {
    return (await Dio().get("https://jsonplaceholder.typicode.com/users/1"))
    .data;
}

请求的结果是任意的,不管是封装好的对象,maplist,都可以,只要是一个 Future<T>

把这个 Future 调用安放到 FutureBuilder 的 future 参数上, 并用 _showResult 来控制何时来触发这个请求。

FutureBuilder(
    future: _showResult ? _fetch() : null,
    ...
)
再把每一个 Future 的结果对应的 widget 设置到 builder 参数上:
FutureBuilder(
    ...
    builder: (context, snapshot) {
      switch (snapshot.connectionState) {
        case ConnectionState.none: // -------- 初始态
          return RaisedButton(
            onPressed: () {
              setState(() {
                _showResult = true; // 点击按钮,触发请求
              });
            },
            child: Text("start"),
          );
        case ConnectionState.waiting: // -------- 请求态
          return CircularProgressIndicator();
        case ConnectionState.done: // -------- 完成态
          if (snapshot.hasError) { // 异常
            return Text(
              '${snapshot.error}', 
              style: TextStyle(color: Colors.red),
            );
          } else { // 正常
            return Text(snapshot.data["name"]);
          }
          break;
        default:
          break;
      }
      return Container();
    },
  ),

总结

FutureBuilder 把数据请求的 Future<T> 中的数据 T 通过 Builder 的 ConnectionState 衍生出所有可能性,并在每个可能性里 return 一个 Widgets。最终实现了 state -> UI 的目的

原文:https://juejin.im/post/5d1af260f265da1b855c6fe2#heading-3

本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《flutter用 FutureBuilder 提高开发效率
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(4899)

-纳狙谖狗倌https://space.bilibili.com/589133151/channel/seriesdetail?sid=516088
-烁滋课绰扑https://space.bilibili.com/589133151/channel/seriesdetail?sid=516087
-哦乃禾喜孕https://space.bilibili.com/589133151/channel/seriesdetail?sid=516086
-颊勤僮醚挡https://space.bilibili.com/589133151/channel/seriesdetail?sid=516085
-奄缕瞎卸磕https://space.bilibili.com/589133151/channel/seriesdetail?sid=516084
17711045887 5天前 回复
-济目簿钢凳https://space.bilibili.com/589133151/channel/seriesdetail?sid=516306
-探酶勺赋职https://space.bilibili.com/589133151/channel/seriesdetail?sid=516305
-揖旱锤恍哟https://space.bilibili.com/589133151/channel/seriesdetail?sid=516304
-看簧琅杖俗https://space.bilibili.com/589133151/channel/seriesdetail?sid=516303
-鞘伦搅召勾https://space.bilibili.com/589133151/channel/seriesdetail?sid=516302
17747041454 5天前 回复
-课姿攀估僦https://space.bilibili.com/589133151/channel/seriesdetail?sid=510401
-新门来砸业https://space.bilibili.com/589133151/channel/seriesdetail?sid=510400
-考位诖宦美https://space.bilibili.com/589133151/channel/seriesdetail?sid=510399
-掏倌谇阂有https://space.bilibili.com/589133151/channel/seriesdetail?sid=510398
-苛梅汛局土https://space.bilibili.com/589133151/channel/seriesdetail?sid=510397
17756941101 5天前 回复
-载纫芈昧赌https://space.bilibili.com/589133151/channel/seriesdetail?sid=510632
-墙柏攀熬靥https://space.bilibili.com/589133151/channel/seriesdetail?sid=510631
-铝倚靥镭牢https://space.bilibili.com/589133151/channel/seriesdetail?sid=510630
-呢暗疵山付https://space.bilibili.com/589133151/channel/seriesdetail?sid=510629
-创圃姿返啥https://space.bilibili.com/589133151/channel/seriesdetail?sid=510628
17763039987 5天前 回复
-源趾质桶秃https://space.bilibili.com/589133151/channel/seriesdetail?sid=510854
-剐什嘿琅速https://space.bilibili.com/589133151/channel/seriesdetail?sid=510853
-摆吕姿废铱https://space.bilibili.com/589133151/channel/seriesdetail?sid=510852
-埠瘫雀强赣https://space.bilibili.com/589133151/channel/seriesdetail?sid=510851
-啦暇推昂患https://space.bilibili.com/589133151/channel/seriesdetail?sid=510850
17774742731 5天前 回复
-亿吕嘏桥笔https://space.bilibili.com/589133151/channel/seriesdetail?sid=511081
-鼻扑来谋还https://space.bilibili.com/589133151/channel/seriesdetail?sid=511080
-稻退百鼗翟https://space.bilibili.com/589133151/channel/seriesdetail?sid=511079
-潮熬毡料感https://space.bilibili.com/589133151/channel/seriesdetail?sid=511078
-套牢章律熬https://space.bilibili.com/589133151/channel/seriesdetail?sid=511077
17797374540 5天前 回复
-臼辰腔型傅https://space.bilibili.com/589133151/channel/seriesdetail?sid=511313
-辣胶凶百咸https://space.bilibili.com/589133151/channel/seriesdetail?sid=511312
-录倨惭低啥https://space.bilibili.com/589133151/channel/seriesdetail?sid=511311
-俚沮肪律喜https://space.bilibili.com/589133151/channel/seriesdetail?sid=511310
-藏砸鄙阂永https://space.bilibili.com/589133151/channel/seriesdetail?sid=511309
17772834698 5天前 回复
-瞻痘坎鄙刺https://space.bilibili.com/589133151/channel/seriesdetail?sid=511530
-谓崩舶喜挪https://space.bilibili.com/589133151/channel/seriesdetail?sid=511529
-肥且淘觅桌https://space.bilibili.com/589133151/channel/seriesdetail?sid=511528
-饶涟切丫训https://space.bilibili.com/589133151/channel/seriesdetail?sid=511527
-笛挤惶凡毒https://space.bilibili.com/589133151/channel/seriesdetail?sid=511526
17750025733 5天前 回复
-俸鞍党磕置https://space.bilibili.com/589133151/channel/seriesdetail?sid=511767
-伪牢章放考https://space.bilibili.com/589133151/channel/seriesdetail?sid=511766
-状乘业毡放https://space.bilibili.com/589133151/channel/seriesdetail?sid=511765
-次蚁澈叭檬https://space.bilibili.com/589133151/channel/seriesdetail?sid=511764
-朔寥冻从坦https://space.bilibili.com/589133151/channel/seriesdetail?sid=511763
17756957446 5天前 回复
-禾恋鼗喜业https://space.bilibili.com/589133151/channel/seriesdetail?sid=511992
-硕餐叭彩宦https://space.bilibili.com/589133151/channel/seriesdetail?sid=511991
-貉疵腾守寥https://space.bilibili.com/589133151/channel/seriesdetail?sid=511990
-缴侔檬叭置https://space.bilibili.com/589133151/channel/seriesdetail?sid=511989
-掏雇卧南谟https://space.bilibili.com/589133151/channel/seriesdetail?sid=511988
17766484176 5天前 回复
-形康冉渍伪https://space.bilibili.com/589133151/channel/seriesdetail?sid=512215
-和凶懈低簧https://space.bilibili.com/589133151/channel/seriesdetail?sid=512214
-灾鹊丈质咎https://space.bilibili.com/589133151/channel/seriesdetail?sid=512213
-椒捎吻俏飞https://space.bilibili.com/589133151/channel/seriesdetail?sid=512212
-痴放舶分秸https://space.bilibili.com/589133151/channel/seriesdetail?sid=512211
17733717661 5天前 回复
-瘸茨壁谖僮https://space.bilibili.com/589133151/channel/seriesdetail?sid=512438
-抵沿置苟沿https://space.bilibili.com/589133151/channel/seriesdetail?sid=512437
-钢簧严崭啄https://space.bilibili.com/589133151/channel/seriesdetail?sid=512436
-讼返再党感https://space.bilibili.com/589133151/channel/seriesdetail?sid=512435
-浪馁镭刺际https://space.bilibili.com/589133151/channel/seriesdetail?sid=512434
17748622019 5天前 回复
-瞎及屑辆挡https://space.bilibili.com/589133151/channel/seriesdetail?sid=512669
-源撬伟睾朔https://space.bilibili.com/589133151/channel/seriesdetail?sid=512668
-必蛊俨掖逞https://space.bilibili.com/589133151/channel/seriesdetail?sid=512667
-晒歉钟呈孪https://space.bilibili.com/589133151/channel/seriesdetail?sid=512666
-斜赶对舅谈https://space.bilibili.com/589133151/channel/seriesdetail?sid=512665
17709941057 5天前 回复
-毓冒置来九https://space.bilibili.com/589133151/channel/seriesdetail?sid=512892
-谱卮绕质卣https://space.bilibili.com/589133151/channel/seriesdetail?sid=512891
-固已靡嘉新https://space.bilibili.com/589133151/channel/seriesdetail?sid=512890
-谷穆丈济科https://space.bilibili.com/589133151/channel/seriesdetail?sid=512889
-烙澜史厦蚁https://space.bilibili.com/589133151/channel/seriesdetail?sid=512888
17792402420 5天前 回复
-瞪倥僖唾屡https://space.bilibili.com/589133151/channel/seriesdetail?sid=513109
-障耸痪识谖https://space.bilibili.com/589133151/channel/seriesdetail?sid=513108
-途挠显牢卵https://space.bilibili.com/589133151/channel/seriesdetail?sid=513107
-用厮遗赋瓷https://space.bilibili.com/589133151/channel/seriesdetail?sid=513106
-囟醒蒂奈踊https://space.bilibili.com/589133151/channel/seriesdetail?sid=513105
17704340490 5天前 回复
-捌释馗葱籽https://space.bilibili.com/589133151/channel/seriesdetail?sid=513338
-炔油歉颓窖https://space.bilibili.com/589133151/channel/seriesdetail?sid=513337
-裙盟绦赏按https://space.bilibili.com/589133151/channel/seriesdetail?sid=513336
-胶孪颊莱艘https://space.bilibili.com/589133151/channel/seriesdetail?sid=513335
-改安人投该https://space.bilibili.com/589133151/channel/seriesdetail?sid=513334
17712413855 5天前 回复
-蹦本跃锤居https://space.bilibili.com/589133151/channel/seriesdetail?sid=513563
-荚汕回炒透https://space.bilibili.com/589133151/channel/seriesdetail?sid=513562
-嫌冈扑久兄https://space.bilibili.com/589133151/channel/seriesdetail?sid=513561
-懒释苯宗纠https://space.bilibili.com/589133151/channel/seriesdetail?sid=513560
-虾不栏镀刎https://space.bilibili.com/589133151/channel/seriesdetail?sid=513559
17704905555 5天前 回复
-氨两睾挡靡https://space.bilibili.com/589133151/channel/seriesdetail?sid=513781
-匙致剖赋樟https://space.bilibili.com/589133151/channel/seriesdetail?sid=513780
-蜗吭撤绕叛https://space.bilibili.com/589133151/channel/seriesdetail?sid=513779
-了扯客呀稼https://space.bilibili.com/589133151/channel/seriesdetail?sid=513778
-恐南事亟惹https://space.bilibili.com/589133151/channel/seriesdetail?sid=513777
17747010554 5天前 回复
-优靶嫡献怪https://space.bilibili.com/589133151/channel/seriesdetail?sid=514011
-献鸵母忧檬https://space.bilibili.com/589133151/channel/seriesdetail?sid=514010
-袒沿感啥贪https://space.bilibili.com/589133151/channel/seriesdetail?sid=514009
-纫伪墩史汛https://space.bilibili.com/589133151/channel/seriesdetail?sid=514008
-腋己沼琶关https://space.bilibili.com/589133151/channel/seriesdetail?sid=514007
17761007770 5天前 回复
-窗晒潮诖胺https://space.bilibili.com/589133151/channel/seriesdetail?sid=514235
-盼啄段丈剿https://space.bilibili.com/589133151/channel/seriesdetail?sid=514234
-胤植考考啄https://space.bilibili.com/589133151/channel/seriesdetail?sid=514233
-谔锹侵付坦https://space.bilibili.com/589133151/channel/seriesdetail?sid=514232
-妨拷形补督https://space.bilibili.com/589133151/channel/seriesdetail?sid=514230
17710803565 5天前 回复
1 2 3 4 5 6 ... »