Flutter上でCloud Functionsを使用しするとき非同期で取得しますが、結果が返ってこないときの対処法を紹介します。
Flutterのバージョン: 1.0.0
TL;DR
cloud_functions
パッケージを使ったCloud Functionsの呼び出しが完了しないpubspec.yaml
のdependenciesにcloud_firestore
を追加する (Cloud Firestoreを使っていなくても)
現象
asyncでcloud_functions
を使ってCloud Functionsのレスポンスを取得しようとしましたが、レスポンス待ちの状態で値が返ってきませんでした。以下のようにFutureBuilderを使いました。
FutureBuilder(
future: CloudFunctions.instance.call(functionName: 'hello'),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
if (snapshot.data != null) {
return new Text(snapshot.data['greeting']);
}
}
return Text('none');
}
)
上記のsnapshot.data
にレスポンスが入るはずですが、非同期処理が完了しないため値が入りません。snapshot
のステータスを確認するとwaitingのまま更新されません。
解決策
根本的な解決方法ではないと思いますが、cloud_firestore
のパッケージを追加することで結果を取得できるようになりました。
pubspec.yaml
dependencies:
flutter:
sdk: flutter
cloud_functions: ^0.0.5
cloud_firestore: ^0.8.2+3 # これを追加します