skip to Main Content

Here is my code of on submit button :

onPressed: () async {
                    if (_formKey.currentState?.validate() == true) {
                      // Do something if the form is valid, for example, submit the data
                      Map<String, dynamic> data = {
                        "Property Title": propertyTitle,
                        "Property Description": propertyDescription,
                        "Bed Rooms": propertyBedRooms,
                        "Bath Rooms": propertyBathRooms,
                        "Property Area": propertyArea,
                        "Property Price": propertyPrice,
                      };
                      final collectionReference = FirebaseFirestore.instance
                          .collection('builderData')
                          .add(data);
                    }
                  },

And here is my main() where i have initialized the firebase:

 void main() {
  //firebase starter
  WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  runApp(const MyApp());
}

2

Answers


  1. Try to replace :

      Map<String, dynamic> data = {
                            "Property Title": propertyTitle,
                            "Property Description": propertyDescription,
                            "Bed Rooms": propertyBedRooms,
                            "Bath Rooms": propertyBathRooms,
                            "Property Area": propertyArea,
                            "Property Price": propertyPrice,
                          };
                          final collectionReference = FirebaseFirestore.instance
                              .collection('builderData')
                              .add(data);
                        }
    

    with the below:

                          final collectionReference = FirebaseFirestore.instance
                              .collection('builderData')
                              .set({
                            "Property Title": propertyTitle,
                            "Property Description": propertyDescription,
                            "Bed Rooms": propertyBedRooms,
                            "Bath Rooms": propertyBathRooms,
                            "Property Area": propertyArea,
                            "Property Price": propertyPrice,}, SetOptions(merge : true));
    

    and for only updating data that already existed do like below

                     final collectionReference = FirebaseFirestore.instance
                          .collection('builderData')
                          .update({
                        "Property Title": propertyTitle,
                        "Property Description": propertyDescription,
                        "Bed Rooms": propertyBedRooms,
                        "Bath Rooms": propertyBathRooms,
                        "Property Area": propertyArea,
                        "Property Price": propertyPrice,});
    
    Login or Signup to reply.
  2. This may work once check :

    Instead of giving the TextControllers, the variables are assigned. later in the TextField() the onChange (value) is given so that if there is any change in the value of the field it gets assigned to the variable and later get into the firebase storage.

        import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:flutter/material.dart';
    import 'package:google_fonts/google_fonts.dart';
    
    import '../strings/colors.dart';
    
    class BuilderAddProperty extends StatefulWidget {
      const BuilderAddProperty({Key? key}) : super(key: key);
    
      @override
      State<BuilderAddProperty> createState() => _BuilderAddPropertyState();
    }
    
    class _BuilderAddPropertyState extends State<BuilderAddProperty> {
      String propertyTitle = '';
      String propertyDescription = '';
      int propertyBedRooms = 0;
      int propertyBathRooms = 0;
      int propertyArea = 0;
      int propertyPrice = 0;
      final _formKey = GlobalKey<FormState>();
    
      String? _validateTitle(String? value) {
        if (value == null || value.isEmpty) {
          return 'Please enter a property title';
        }
        return null;
      }
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: Scaffold(
            body: SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.all(30),
                child: Form(
                  key: _formKey,
                  child: Column(
                    children: [
                      TextFormField(
                        onChanged: (values) {
                          propertyTitle = values;
                        },
                        decoration: const InputDecoration(
                            labelText: 'Property Title',
                            hintText: 'Enter Property Title',
                            border: OutlineInputBorder()),
                        validator: _validateTitle,
                      ),
                      const SizedBox(height: 20),
                      TextFormField(
                        onChanged: (values) {
                          propertyDescription = values;
                        },
                        keyboardType: TextInputType.multiline,
                        maxLines: 5,
                        decoration: const InputDecoration(
                            labelText: 'Property Description',
                            hintText: 'Enter Property Description',
                            border: OutlineInputBorder()),
                        validator: _validateTitle,
                      ),
                      const SizedBox(height: 20),
                      SizedBox(
                        height: 50,
                        width: double.infinity,
                        child: ElevatedButton.icon(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: rNavyGrey,
                          ),
                          onPressed: () async {},
                          icon: Icon(Icons.upload),
                          label: Text(
                            'Upload Images From Gallery',
                            style: GoogleFonts.poppins(
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(height: 20),
                      TextFormField(
                        onChanged: (values) {
                          propertyBedRooms = values as int;
                        },
                        keyboardType: TextInputType.number,
                        decoration: const InputDecoration(
                            labelText: 'No of Bed Rooms',
                            hintText: 'Enter Number Of Bed Rooms',
                            border: OutlineInputBorder()),
                        validator: _validateTitle,
                      ),
                      const SizedBox(height: 20),
                      TextFormField(
                        onChanged: (values) {
                          propertyBathRooms = values as int;
                        },
                        keyboardType: TextInputType.number,
                        decoration: const InputDecoration(
                            labelText: 'No of Bath Rooms',
                            hintText: 'Enter Number Of Bath Rooms',
                            border: OutlineInputBorder()),
                        validator: _validateTitle,
                      ),
                      const SizedBox(height: 20),
                      TextFormField(
                        onChanged: (values) {
                          propertyArea = values as int;
                        },
                        keyboardType: TextInputType.number,
                        decoration: const InputDecoration(
                            labelText: 'Area (in sqft)',
                            hintText: 'Enter Area In Sqft',
                            border: OutlineInputBorder()),
                        validator: _validateTitle,
                      ),
                      const SizedBox(height: 20),
                      TextFormField(
                        onChanged: (values) {
                          propertyPrice = values as int;
                        },
                        keyboardType: TextInputType.number,
                        decoration: const InputDecoration(
                            labelText: 'Price (in Rupees)',
                            hintText: 'Enter Price Of The Listing',
                            border: OutlineInputBorder()),
                        validator: _validateTitle,
                      ),
                      const SizedBox(height: 20),
                      SizedBox(
                        height: 50,
                        width: double.infinity,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: rNavyGrey,
                          ),
                          onPressed: () async {
                            if (_formKey.currentState?.validate() == true) {
                              CollectionReference collRef = FirebaseFirestore
                                  .instance
                                  .collection('builderData');
                              // Do something if the form is valid, for example, submit the data
                              collRef.add({
                                "Property Title": propertyTitle,
                                "Property Description": propertyDescription,
                                "Bed Rooms": propertyBedRooms,
                                "Bath Rooms": propertyBathRooms,
                                "Property Area": propertyArea,
                                "Property Price": propertyPrice,
                              });
                            }
                          },
                          child: Text(
                            'Submit Now',
                            style: GoogleFonts.poppins(
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search