'The method "collection" isn't defined for the type FirebaseStorage
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());
}
}
} ```
Solution 1:[1]
Replace firebase.collection
with :
FirebaseFirestore.instance.collection
And import Firestore. It's a different thing from storage.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Huthaifa Muayyad |