skip to Main Content

Flutter beginner here. Working on a flutter project where I can submit a form where I can upload it’s content in firebase. I took the data from TextFormField and DropDownButton and the images from ImagePicker. I can upload the image file perfectly to the firebase but the data are not uploading. Here is the code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart' as path;

class AddDoctor extends StatefulWidget {
  @override
  State<AddDoctor> createState() => AddDoctorState();
}

class AddDoctorState extends State<AddDoctor> {
  late String name;
  late int age;
  late String description;
  String specialistValue = 'Select Specialist';
  String hospitalValue = 'Select Hospital';
  List<String> imageUrlList = [];

  final controllerName = TextEditingController();
  final controllerAge = TextEditingController();
  final controllerDesciption = TextEditingController();

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final FirebaseStorage _firebaseStorage = FirebaseStorage.instance;
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;
  final ImagePicker _picker = ImagePicker();
  XFile? image;

  void pickDoctorImage() async {
    try {
      final pickedImage = await _picker.pickImage(source: ImageSource.gallery);

      setState(() {
        image = pickedImage!;
      });
    } catch (e) {}
  }

  Widget displayImage() {
    return Image.file(File(image!.path));
  }

  

  Future<void> uploadImage() async {
    Reference ref =
        _firebaseStorage.ref('products/${path.basename(image!.path)}');

    await ref.putFile(File(image!.path)).whenComplete(() async {
      await ref.getDownloadURL().then((value) {
        imageUrlList.add(value);
      });
    });
  }

  void uploadInfo() async {
    CollectionReference infoRef = _firestore.collection('DoctorList');

    await infoRef.doc().set({
      'name': name,
      'age': age,
      'description': description,
      'specialist': specialistValue,
      'hospital': hospitalValue,
      'doctorImage': imageUrlList,
    }).whenComplete(() {
      Navigator.pop(context);
    });
  }

  void uploadDoctorInfo() async {
    await uploadImage().whenComplete(() => uploadInfo);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFD9E4EE),
      appBar: AppBar(
        title: const Text('Add Doctor'),
        actions: [
          IconButton(
            icon: const Icon(Icons.menu),
            onPressed: () {},
          ),
        ],
      ),
      body: Form(
        key: _formKey,
        child: ListView(
          padding: const EdgeInsets.all(16),
          children: <Widget>[
            TextFormField(
              keyboardType: TextInputType.name,
              validator: (value) {
                if (value!.isEmpty) {
                  return 'Please Name must not be empty';
                } else {
                  return null;
                }
              },
              controller: controllerName,
              decoration: const InputDecoration(
                label: Text('Name'),
              ),
              onSaved: (value) {
                name = value!;
              },
            ),
            const SizedBox(height: 10),
            TextFormField(
              keyboardType: TextInputType.number,
              validator: (value) {
                if (value!.isEmpty) {
                  return 'Please Age must not be empty';
                } else {
                  return null;
                }
              },
              controller: controllerAge,
              decoration: const InputDecoration(
                label: Text('Age'),
              ),
              onSaved: (value) {
                age = int.parse(value!);
              },
            ),
            const SizedBox(height: 10),
            DropdownButton(
              borderRadius: BorderRadius.circular(30),
              value: specialistValue,
              items: specialistList.map<DropdownMenuItem<String>>((e) {
                return DropdownMenuItem(
                  value: e,
                  child: Text(e),
                );
              }).toList(),
              onChanged: (String? value) {
                setState(() {
                  specialistValue = value!;
                });
              },
            ),
            DropdownButton(
              borderRadius: BorderRadius.circular(30),
              value: hospitalValue,
              items: hospitalList.map<DropdownMenuItem<String>>((e) {
                return DropdownMenuItem(
                  value: e,
                  child: Text(e),
                );
              }).toList(),
              onChanged: (String? value) {
                setState(() {
                  hospitalValue = value!;
                });
              },
            ),
            const SizedBox(height: 10),
            TextFormField(
              keyboardType: TextInputType.number,
              validator: (value) {
                if (value!.isEmpty) {
                  return 'Please Description must not be empty';
                } else {
                  return null;
                }
              },
              maxLength: 100,
              maxLines: 3,
              controller: controllerDesciption,
              decoration: const InputDecoration(
                label: Text('Description'),
              ),
              onChanged: (value) {
                description = value;
              },
            ),
            const SizedBox(height: 10),
            // CircleAvatar(
            //   radius: 50,
            //   backgroundImage: image != null ? FileImage(image) : null,
            // ),
            InkWell(
              onTap: () {
                setState(() {
                  image = null;
                });
              },
              child: Container(
                  padding: const EdgeInsetsDirectional.only(top: 60),
                  height: 150,
                  width: 150,
                  decoration: const BoxDecoration(
                    color: Colors.blue,
                    shape: BoxShape.circle,
                  ),
                  child: Center(
                    child: image != null
                        ? displayImage()
                        : const Text(
                            'You have not pick any image',
                            style: TextStyle(fontSize: 11),
                            textAlign: TextAlign.center,
                          ),
                  )),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: () {
                pickDoctorImage();
              },
              child: const Text('Upload Image'),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              child: const Text('Submit'),
              onPressed: () {
                uploadDoctorInfo();
              },
            ),
          ],
        ),
      ),
    );
  }
}

There aren’t any error in the file either. I can’t figure out where the source of the problem is.

2

Answers


  1. Chosen as BEST ANSWER

    Fixed it. The problem was in

    void uploadDoctorInfo() async {
    await uploadImage().whenComplete(() => uploadInfo);
    }
    

    I changed it to

    void uploadDoctorInfo() async {
    await uploadImage().whenComplete(uploadInfo);
    }
    

    And now it's working fine


  2. You should add for the new entry instead set data in collection. Try using the following code

      void uploadInfo() async {
        CollectionReference infoRef = _firestore.collection('DoctorList');
    
        await infoRef.add({
          'name': name,
          'age': age,
          'description': description,
          'specialist': specialistValue,
          'hospital': hospitalValue,
          'doctorImage': imageUrlList,
        }).whenComplete(() {
          Navigator.pop(context);
        });
      }
    

    Edited

    Your are referencing uploadInfo function instead of call that. updateInfo should be called by adding (), so uploadDoctorInfo will be look like.

    void uploadDoctorInfo() async {
        await uploadImage().whenComplete(() => uploadInfo());
      }
    

    or

    void uploadDoctorInfo() async {
        await uploadImage().whenComplete(uploadInfo);
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search