Flutter 開発の実践 - FutureBuilder を使用してウィジェットを非同期的に更新する
開発プロセスでは、画像をダウンロードした後にウィジェットを表示し、特定のデータを取得したら対応する UI に表示するなど、ウィジェットを更新するために非同期データに依存する必要がある状況によく遭遇します。 FutureBuilder を使用してウィジェットを非同期的に更新できます。
1.フューチャービルダー
FutureBuilder は、Future との対話の最新のスナップショットに基づいて構築されるウィジェットです。
/// 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);
で
-
future:final Future? future; は非同期タスクです。
-
builder: Final AsyncWidgetBuilder builder; 表示されるウィジェットを作成します。表示されるウィジェットは AsyncSnapshot<String?> スナップショットに基づいて決定でき、Future の実行中に複数回呼び出すことができます。
2.FutureBuilderを使用する
FutureBuilder の使用例です。Web ページを読み込むときに、Webview に Cookie を設定し、Cookie にトークンを設定する必要があります。トークンを取得して、Webview の Cookie に設定する必要があります。
トークンの取得
Future<String?> _getToken() async {
final token = (await SessionDataService.sessionData)?.token;
if (token == null) return null;
return token;
}
トークンを取得した後、FutureBuilder を使用して Webview を更新し、最初に snapshot.hasData にデータがあるかどうかを確認します。データがある場合はWebviewを直接表示し、データがない場合はデフォルトのコンテナが表示されます。
FutureBuilder<String?>(
future: _getToken(),
builder: (BuildContext context, AsyncSnapshot<String?> 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<String?> _getToken() async {
// final token = (await SessionDataService.sessionData)?.token;
final token = ApiAuth().token;
if (token == null) return null;
return token;
}
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Container(
color: Colors.amber,
),
SafeArea(
bottom: false,
child: FutureBuilder<String?>(
future: _getToken(),
builder: (BuildContext context, AsyncSnapshot<String?> 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();
},
),
),
],
),
);
}
}
3. まとめ
Flutter 開発の実践 - FutureBuilder を使用して非同期データでウィジェットを更新します。説明があまり正確ではないかもしれませんが、ご容赦ください。
https://blog.csdn.net/gloryFlow/article/details/133490457
勉強して記録し、日々改善を続けてください。