web-dev-qa-db-ja.com

Flutterを使用してFirebaseに画像をアップロードする方法

私はimage_pickerライブラリを使用しています https://pub.dartlang.org/packages/image_picker と一緒にアップロードしたい写真を選択/撮影します。これまでのコードは、Firebaseストレージにイメージを正常にアップロードします。唯一の問題は、イメージがアップロードされた後、アプリがシャットダウンすることです(実際にはクラッシュせず、閉じるだけで、VSコードがデバイスへの接続を失います)。コードは次のとおりです。

 File _image;

  Future _takeProfilePicture() async{
    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    setState((){
      _image = image;
    });
  }

  Future _selectProfilePicture() async{
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);

    setState((){
      _image = image;
    });
  }

  Future<Null> _uploadProfilePicture() async{
    FirebaseUser user = await FirebaseAuth.instance.currentUser();

    final StorageReference ref = FirebaseStorage.instance.ref().child('${user.email}/${user.email}_profilePicture.jpg');
    final StorageUploadTask uploadTask = ref.putFile(_image);
    final Uri downloadUrl = (await uploadTask.future).downloadUrl;
  }

  void _selectAndUploadPicture() async{
    await _selectProfilePicture();
    await _uploadProfilePicture();
  }

  void _takeAndUploadPicture() async{
    await _takeProfilePicture();
    await _uploadProfilePicture();
  }

そして、端末は次を印刷します。

W/Firestore( 6873): (0.6.6-dev) [Firestore]: The behavior for Java.util.Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.
W/Firestore( 6873): To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
W/Firestore( 6873):
W/Firestore( 6873): FirebaseFirestore firestore = FirebaseFirestore.getInstance();
W/Firestore( 6873): FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
W/Firestore( 6873):     .setTimestampsInSnapshotsEnabled(true)
W/Firestore( 6873):     .build();
W/Firestore( 6873): firestore.setFirestoreSettings(settings);
W/Firestore( 6873):
W/Firestore( 6873): With this change, timestamps stored in Cloud Firestore will be read back as com.google.firebase.Timestamp objects instead of as system Java.util.Date objects. So you will also need to update code expecting a Java.util.Date to instead expect a Timestamp. For example:
W/Firestore( 6873):
W/Firestore( 6873): // Old:
W/Firestore( 6873): Java.util.Date date = snapshot.getDate("created_at");
W/Firestore( 6873): // New:
W/Firestore( 6873): Timestamp timestamp = snapshot.getTimestamp("created_at");
W/Firestore( 6873): Java.util.Date date = timestamp.toDate();
W/Firestore( 6873):
W/Firestore( 6873): Please audit all existing usages of Java.util.Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.

提案されたJava端末からのコードを実装しようとしましたが、cloud_firestoreライブラリを使用してflutterで同等のコードを記述する方法を見つけることができないようです https://pub.dartlang.org/packages/cloud_firestore 、FirebaseFirestoreSettingsに相当するものがありません(または、見つけることができないようです)。

前もって感謝します!

8
Marko

Firebase_storageプラグインの1.0.4バージョンで動作するmmccabeの回答の更新バージョン

Future<String> _pickSaveImage(String imageId) async {
  File imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
  StorageReference ref =
    FirebaseStorage.instance.ref().child(imageId).child("image.jpg");
  StorageUploadTask uploadTask = ref.putFile(imageFile);
  return await (await uploadTask.onComplete).ref.getDownloadURL();
}

現時点では、次のことを行う必要があります。

var downloadURL =(auploadTask.onComplete).ref.getDownloadURL();

なぜなら エラー:ゲッター 'future'はクラス 'StorageUploadTask'に定義されていないからです(undefined_getter at [timetracker] lib/screens/image_detection.Dart:63)

8
Mans

次は私のために働く:

Future<Uri> _pickSaveImage(String imageId) async {
  File imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
  StorageReference ref =
    FirebaseStorage.instance.ref().child(imageId).child("image.jpg");
  StorageUploadTask uploadTask = ref.putFile(imageFile);
  return (await uploadTask.future).downloadUrl;
}
2
mmccabe
Future<String> _pickSaveImage(String imageId) async {
  File imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
  StorageReference ref =
    FirebaseStorage.instance.ref().child(imageId).child("image.jpg");
  StorageUploadTask uploadTask = ref.putFile(imageFile);
  return await (await uploadTask.onComplete).ref.getDownloadURL();
}

今のところ、

これはエラーになります:

return await(await uploadTask.onComplete).ref.getDownloadURL();

これはしません:

return(auploadTask.onComplete).ref.getDownloadURL();

エラーメッセージ:

The getter 'future' isn't defined for the class 'StorageUploadTask'. 

undefined_getter at [timetracker] lib/screens/image_detection.Dart:63)