我是新手。我正在创建一个将用户数据存储在Firestore中的应用程序,用户也可以更新它。该应用程序可以将数据存储在Firestore中,并可以从中检索数据,但在更新过程中会引发错误
I/flutter (15042): NoSuchMethodError: The method 'existsSync' was called on null.
I/flutter (15042): Receiver: null
I/flutter (15042): Tried calling: existsSync()
我下面使用的代码未显示任何编译错误,但仅在更新数据时抛出错误
这是用户添加数据并显示检索数据以进行编辑的表格。
class AddProductsForm extends StatefulWidget {
AddProductsForm(
{this.submitFn,
this.initTitle,
this.initCollection,
this.initPrice,
this.initQuantity,
this.initLiters,
this.initImage});
final void Function(
String productTitle,
String price,
String quantity,
String category,
String liters,
File image,
BuildContext ctx,
bool update,
) submitFn;
final initTitle;
final initCollection;
final initPrice;
final initQuantity;
final initLiters;
final initImage;
@override
_AddProductsFormState createState() => _AddProductsFormState();
}
class _AddProductsFormState extends State<AddProductsForm> {
final _formKey = GlobalKey<FormState>();
String _title = '';
String _price = '';
String _quantity = '';
var _category;
String _liters = '';
File _image;
var _update = false;
void _updateValidator() {
FocusScope.of(context).unfocus();
final isValid = _formKey.currentState.validate();
if(isValid){
_formKey.currentState
.save();
}
}
void _updateSubmit() {
widget.submitFn(
_title,
_price,
_quantity,
_category,
_liters,
_image,
context,
_update,
);
}
void _trySubmit() {
FocusScope.of(context).unfocus();
final isValid = _formKey.currentState.validate();
if (isValid) {
_formKey.currentState
.save(); // this will go and trigger all the onSaved in the TextFormField
if (_image == null) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('Please pick an Image'),
backgroundColor: Theme.of(context).errorColor,
),
);
return;
}
widget.submitFn(
_title,
_price,
_quantity,
_category,
_liters,
_image,
context,
_update,
);
}
}
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Center(
child: Card(
elevation: 15,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(
padding: EdgeInsets.all(20),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
fit: FlexFit.loose,
child: TextFormField(
initialValue:
widget.initTitle == 'App Bar' ? null : widget.initTitle,
decoration: InputDecoration(
labelText: 'Enter Product Name',
contentPadding: EdgeInsets.all(5)),
validator: (value) {
if (value.isEmpty) {
return 'Enter a the Product Title';
}
return null;
},
keyboardType: TextInputType.text,
onSaved: (value) {
_title = value;
}, //this (value) in the (arguments) is the value given by the user
),
),
SizedBox(
height: 30,
),
Container(
padding: EdgeInsets.only(top: 25),
alignment: Alignment.bottomRight,
child: FlatButton.icon(
onPressed: () {
_updateValidator();
print(_quantity);
print(widget.initQuantity);
if(widget.initTitle != _title )
{
setState(() {
_update = true;
});
_cmonSubmit();
print(_update);
print(_quantity);
print(widget.initQuantity);
setState(() {
_update = false;
});
}else{
_trySubmit();
}
Navigator.of(context).pop();
},
icon: Icon(Icons.save),
label: Text('Save'),
color: Colors.grey,
),
),
下面的代码更新Firestore中的文档字段
在控制台上没有打印打印(更新)。 (更新是一个布尔值)
void _submitProductForm(
String productTitle,
String price,
String quantity,
String category,
var liters,
File image,
BuildContext ctx,
bool update,
) async {
print(update);
try{
final ref = FirebaseStorage.instance
.ref()
.child('product_Image')
.child(productTitle + '.jpg');
await ref.putFile(image).onComplete;
final url = await ref.getDownloadURL();
if(update == true){
Firestore.instance.collection(category).document(productTitle).updateData({
'title': productTitle,
'price': price,
'quantity': quantity,
'category': category,
'image':url,
'liters': liters,
'update': 'true',
}).catchError((error){
print(error);
}).then((value) => print('updated'),);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: AddProductsForm(
submitFn: _submitProductForm,
),
],
),
);
}
}