Home > Enterprise >  Passing a URL file to firebase using flutter
Passing a URL file to firebase using flutter

Time:01-02

I'm attempting to upload URL posterImg in the createGroup method but I get the following error "The argument type 'String' can't be assigned to the parameter type 'File"

To provide more context, I'm currently capturing the URL of images stored online using the variable posterImg, I then want to convert the path to a file without storing the image locally and then saving it to a file so it can be uploaded to firebase.

In the snippet below I have tried casting posterImg to image (which is of the type file) but it fails.

Can anyone advise how to modify my code to pass a URL stored in posterImg to the Firebase??

Please can anyone share the festive sprite and up with this problem, as I'm pulling my hair out.

class _CreateGroupScreenState extends ConsumerState<CreateGroupScreen> {
  final TextEditingController groupNameController = TextEditingController();

  File? image;
  late String name;
  late String posterImg = "";

  void selectImage2() async {
    image = await pickImageFromGallery(context);
    setState(() {});
  }


  Future<void> createGroup() async {
    bool groupLive;

    if (await groupExist(context)) {
      groupLive = true;
    } else {
      groupLive = false;
    }


    if (!groupLive && groupNameController.text
        .trim()
        .isNotEmpty && image != null) {
      ref.read(groupControllerProvider).createGroup(
        context,
        name,
        posterImg!,
        ref.read(selectedGroupContacts),
      );
      ref.read(selectedGroupContacts.state).update((state) => []);
      Navigator.pop(context);
    }
  }

  @override
  void dispose() {
    super.dispose();
    groupNameController.dispose();
  }

  Future<bool> groupExist(context) async {
    var groupRepository = ref.read(groupRepositoryProvider);
    var groupExists = await groupRepository.groupExists(widget.groupContainer.mid);
    return groupExists;
  }


  @override
  Widget build(BuildContext context) {
    name = widget.groupContainer.name;    

    if (widget.groupContainer.img != null) {
      posterImg = widget.groupContainer.img;
    } else {
      posterImg = '';
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text('Create Group'),
      ),
      body: Center(
        child: Column(
          children: [
            const SizedBox(height: 10),
            Stack(
              children: [
                image == null
                    ? const CircleAvatar(
                  //backgroundImage: NetworkImage(posterImg),
                  backgroundImage: AssetImage('assets/nobody.png'),
                  //backgroundImage: AssetImage(Assets),
                  radius: 64,
                )
                    : CircleAvatar(
                  backgroundImage: FileImage(
                    // posterImg,
                    //widget.groupContainer.img,
                    image!,
                  ),
                  radius: 64,
                ),
                Positioned(
                  bottom: -10,
                  left: 80,
                  child: IconButton(
                    onPressed: selectImage,
                    icon: const Icon(
                      Icons.add_a_photo,
                    ),
                  ),
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: TextField(
                controller: groupNameController,
                decoration: const InputDecoration(
                  hintText: 'Enter Group Name',
                ),
              ),
            ),
            Container(
              alignment: Alignment.topLeft,
              padding: const EdgeInsets.all(8),
              child: const Text(
                'Select Contacts',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
            const SelectContactsGroup(),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: createGroup,
        backgroundColor: tabColor,
        child: const Icon(
          Icons.done,
          color: Colors.white,
        ),
      ),
    );
  }
}

CodePudding user response:

There is no way to upload a file to Cloud Storage through the Firebase SDKs from a URL. The only options are to upload from a local file, a blob, or a base64 encoded string with the binary data.

If you have only a URL of the file you want to store in Cloud Storage, you will have to load the data into your application and from there upload it to Cloud Storage.

Also see:

  • Related