Home > OS >  How can i store the url of the image instead of path of the image in firestore flutter
How can i store the url of the image instead of path of the image in firestore flutter

Time:06-14

I want to store the URL of the image like this -

this is how i want to store url of the image in firestore

This is my current condition -

Currently it's showing the path of the image not the url

   onPressed: () async {
          if (imageFile == null) {
            print("error");
          } else {
            try {
              final ref = storage
                  .ref()
                  .child('user_profile_images')
                  .child(name   '.jpg');
              ref.putFile(imageFile!);
              url = await ref.getDownloadURL();
              if (url != null) {
                print(url);
              }
            } catch (e) {
              print(e);
            }

            dynamic user = await auth.createUserWithEmailAndPassword(
                email: email, password: password);
            await FirebaseFirestore.instance
                .collection('users')
                .doc(name)
                .set({
              'email': email,
              'name': name,
              'profileurl': imageFile?.path,
              'userid': auth.currentUser?.uid
            }).then((value) {
              try {
                if (user != null) {
                  Navigator.pushNamed(context, '/dashboard');
                }
              } catch (e) {
                print(e);
              }
            });
          }
        }),

CodePudding user response:

'profileurl': imageFile?.path, => 'profileurl': url?.toString(),

CodePudding user response:

I have a feeling the multiple try/catch blocks are getting in your way. In addition, it is never a good idea to mix then and await in a single block of code.

Combining those, try the following:

try {
  final ref = storage
      .ref()
      .child('user_profile_images')
      .child(name   '.jpg');
  ref.putFile(imageFile!);
  url = await ref.getDownloadURL();
  if (url != null) {
    print(url);
  }

  dynamic user = await auth.createUserWithEmailAndPassword(
    email: email, password: password);
  dynamic value = await FirebaseFirestore.instance
    .collection('users')
    .doc(name)
    .set({
      'email': email,
      'name': name,
      'profileurl': url //            
  • Related