这是我的提交按钮代码:
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);
}
},
这里是我的main(),我已经初始化了firebase:
void main() {
//firebase starter
WidgetsFlutterBinding.ensureInitialized();
Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
尝试替换:
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);
}
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));
,如果只更新已经存在的数据,请执行下面的命令
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,});
这可能工作一次检查:
变量被赋值,而不是给出TextControllers。之后在TextField()中给出了onChange (value),这样如果字段的值有任何变化,它就会被分配给变量,然后进入firebase存储。
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,
),
),
),
),
],
),
),
),
),
),
);
}
}