• 作者:老汪软件
  • 发表时间:2023-12-25 08:00
  • 浏览量:

开发实战-使用异步数据更新

在开发过程中,经常遇到需要依赖异步数据更新的情况,如下载图片后显示,获取到某个数据时候,显示在对应的UI界面上,都可以使用异步数据更新。

一、

是一个,该基于与]交互的最新快照构建的。

/// Creates a widget that builds itself based on the latest snapshot of
  /// interaction with a [Future].
  ///
  /// The [builder] must not be null.
  const FutureBuilder({
    super.key,
    this.future,
    this.initialData,
    required this.builder,
  }) : assert(builder != null);

其中

二、使用

这里使用的示例,我是通过加载网页时候,需要将中设置,中需要设置token。token需要获取到再设置到中的中。

获取token

Future _getToken() async {
    final token = (await SessionDataService.sessionData)?.token;
    if (token == null) return null;
    return token;
  }

使用用来在获取token后更新,先判断.是否有数据。如果有数据,则直接显示,如果没有数据,则显示默认的。

FutureBuilder(
              future: _getToken(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.hasData) {
                  final token = snapshot.data;
                  if (token == null) return Container();
                  return WebView(
                    javascriptMode: JavascriptMode.unrestricted,
                    initialUrl: url,
                    initialCookies: [
                      WebViewCookie(
                          name: "auth", value: "token", domain: "inice.cn"),
                    ],
                    userAgent: "inice.cn",
                    onWebViewCreated: (controller) {
                      cookieManager.setCookies([
                        Cookie('auth', token)
                          ..domain = 'inice.cn'
                          ..httpOnly = false,
                      ]);
                      webController = controller;
                    },
                  );
                }
                return Container();
              },
            ),

完整代码如下

class WebViewScreen extends StatelessWidget {
  WebViewScreen({Key? key, required this.url}) : super(key: key);
  final String url;
  WebViewController? webController;
  final cookieManager = WebviewCookieManager();
  Future _getToken() async {
    // final token = (await SessionDataService.sessionData)?.token;
    final token = ApiAuth().token;
    if (token == null) return null;
    return token;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            color: Colors.amber,
          ),
          SafeArea(
            bottom: false,
            child: FutureBuilder(
              future: _getToken(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.hasData) {
                  final token = snapshot.data;
                  if (token == null) return Container();
                  return WebView(
                    javascriptMode: JavascriptMode.unrestricted,
                    initialUrl: url,
                    initialCookies: [
                      WebViewCookie(
                          name: "auth", value: "token", domain: "inice.cn"),
                    ],
                    userAgent: "inice.cn",
                    onWebViewCreated: (controller) {
                      cookieManager.setCookies([
                        Cookie('auth', token)
                          ..domain = 'inice.cn'
                          ..httpOnly = false,
                      ]);
                      webController = controller;
                    },
                  );
                }
                return Container();
              },
            ),
          ),
        ],
      ),
    );
  }
}

三、小结

开发实战-使用异步数据更新。描述可能不是特别准确,请见谅。

学习记录,每天不停进步。