方法“收集”为Firebasestorage类型定义的IS不定义

发布于 2025-01-28 18:59:29 字数 2131 浏览 4 评论 0原文

试图将数据存储在Firestore中,我会遇到错误。另一个问题是在Android Studio(Pixel 5)中使用我的仿真器,甚至使用我的所有4个连接设备。我可以跑步,但他们什么都没有表现出来。 Flutter医生发现没有问题。请帮助我tryna做一个我看不到的应用程序,而是利用我对它的外观的想象,这不是正确的方法。

import 'package:firebase_storage/firebase_storage.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:swift_mobile/constants.dart';
import 'user_model.dart' as model;

class AuthController extends GetxController{

  static AuthController instance = Get.find();

  late Rx<File?> _pickedImage;

  File? get profilePicture => _pickedImage.value;

  void pickAnImage() async {
  final pickedImage = await ImagePicker().pickImage(source: ImageSource.gallery);
  if(pickedImage!=null) {
    Get.snackbar('Profile Picture', 'You\'re good to go!');
   }
   _pickedImage = Rx<File?>(File(pickedImage!.path));
  }

  Future<String> _uploadToStorage(File image) async {
    Reference ref = firebaseStorage
        .ref().child('profilePictures')
        .child(firebaseAuth
        .currentUser!.uid
    );
    UploadTask uploadTusk = ref.putFile(image);
    TaskSnapshot snap = await uploadTusk;
    String downloadUrl = await snap.ref.getDownloadURL();
    return downloadUrl;
  }

  void registerUser(String username, String email, String password, File? image) async {
    try{
      if(username.isNotEmpty &&
          email.isNotEmpty &&
          password.isNotEmpty &&
          image!=null) {
       UserCredential cred =  await firebaseAuth.createUserWithEmailAndPassword(
           email: email, password: password);
        String downloadUrl = await _uploadToStorage(image);
        model.User user = model.User(name: username, email: email, uid: cred.user!.uid, profilePic: downloadUrl);

        await firestore.collection('user').doc(cred.user!.uid).set(user.toJson());
      } else {
        Get.snackbar(
            'Error creating account',
            'Please enter all fields',
        );
      }
    } catch(e) {
      Get.snackbar(
          'Error creating account',
          e.toString());
    }
  }
} ```

I'm getting an error trying to store data in firestore. Another issue is using my emulator in android studio ( pixel 5 ) or even all my 4 connected devices. I can run but they all show nothing. Flutter doctor finds no issues. Please help I'm tryna make an application I can't see but rather use my imagination of how it'd looks and this is not the correct way.

import 'package:firebase_storage/firebase_storage.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:swift_mobile/constants.dart';
import 'user_model.dart' as model;

class AuthController extends GetxController{

  static AuthController instance = Get.find();

  late Rx<File?> _pickedImage;

  File? get profilePicture => _pickedImage.value;

  void pickAnImage() async {
  final pickedImage = await ImagePicker().pickImage(source: ImageSource.gallery);
  if(pickedImage!=null) {
    Get.snackbar('Profile Picture', 'You\'re good to go!');
   }
   _pickedImage = Rx<File?>(File(pickedImage!.path));
  }

  Future<String> _uploadToStorage(File image) async {
    Reference ref = firebaseStorage
        .ref().child('profilePictures')
        .child(firebaseAuth
        .currentUser!.uid
    );
    UploadTask uploadTusk = ref.putFile(image);
    TaskSnapshot snap = await uploadTusk;
    String downloadUrl = await snap.ref.getDownloadURL();
    return downloadUrl;
  }

  void registerUser(String username, String email, String password, File? image) async {
    try{
      if(username.isNotEmpty &&
          email.isNotEmpty &&
          password.isNotEmpty &&
          image!=null) {
       UserCredential cred =  await firebaseAuth.createUserWithEmailAndPassword(
           email: email, password: password);
        String downloadUrl = await _uploadToStorage(image);
        model.User user = model.User(name: username, email: email, uid: cred.user!.uid, profilePic: downloadUrl);

        await firestore.collection('user').doc(cred.user!.uid).set(user.toJson());
      } else {
        Get.snackbar(
            'Error creating account',
            'Please enter all fields',
        );
      }
    } catch(e) {
      Get.snackbar(
          'Error creating account',
          e.toString());
    }
  }
} ```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2025-02-04 18:59:29

firebase.Collection用:

FirebaseFirestore.instance.collection

进口firestore。这与存储是不同的。

Replace firebase.collection with :

FirebaseFirestore.instance.collection

And import Firestore. It's a different thing from storage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文