Firebase 颤振:解决执行updateData时出错,未找到:没有要更新的文档
当我尝试更新firebase数据库中的用户数据时,出现以下错误Firebase 颤振:解决执行updateData时出错,未找到:没有要更新的文档,firebase,flutter,google-cloud-firestore,flutter-change-notifier,Firebase,Flutter,Google Cloud Firestore,Flutter Change Notifier,当我尝试更新firebase数据库中的用户数据时,出现以下错误 E/flutter ( 4756): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(Error performing updateData, NOT_FOUND: No document to update: projects/(DBname)/databases/(default)/documents/user
E/flutter ( 4756): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(Error performing updateData, NOT_FOUND: No document to update: projects/(DBname)/databases/(default)/documents/userData/ckua6TCjc2Dx76W6efuV, null)
这是我的UserData模型类
class UserData {
// String userId;
String id;
String firstName;
String lastName;
String phoneNumber;
String role;
String streetAddress;
String city;
String state;
String postcode;
String country;
Timestamp createdAt;
Timestamp updatedAt;
UserData();
UserData.fromMap(Map<String, dynamic> data) {
id = data['id'];
// userId = data['user_id'];
firstName = data['first_name'];
lastName = data['last_name'];
phoneNumber = data['phone_number'];
role = data['role'];
streetAddress = data['street_address'];
city = data['city'];
postcode = data['postcode'];
state = data['state'];
country = data['country'];
createdAt = data['created_at'];
updatedAt = data['updated_at'];
}
Map<String, dynamic> toMap() {
return {
'id': id,
// 'user_id': userId,
'first_name': firstName,
'last_name': lastName,
'phone_number': phoneNumber,
'role': role,
'street_address': streetAddress,
'city': city,
'postcode': postcode,
'state': state,
'country': country,
'created_at': createdAt,
'updated_at': updatedAt,
};
}
}
这是我的编辑配置文件屏幕,可以调用我的userNotifier
class ProfileFormScreen extends StatefulWidget {
static const String id = 'profile_form';
@override
_ProfileFormScreenState createState() => _ProfileFormScreenState();
}
class _ProfileFormScreenState extends State<ProfileFormScreen> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
UserData _userData;
//global declarations
String selectedBusinessTypeDropDownValue = 'Fashion';
String selectedCurrencyDropDownValue = 'NGN: Nigerian Naira';
String selectedCountryDropDownValue = 'Nigeria';
String email;
_saveUserData(BuildContext context) {
UserDataNotifier userNotifier =
Provider.of<UserDataNotifier>(context, listen: false);
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
userNotifier.updateUserData(_userData, true);
Navigator.pop(context);
}
@override
void initState() {
super.initState();
UserDataNotifier userDataNotifier =
Provider.of<UserDataNotifier>(context, listen: false);
if (userDataNotifier.loggedInUserData != null) {
_userData = userDataNotifier.loggedInUserData;
} else {
_userData = UserData();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Profile'),
),
body: SingleChildScrollView(
padding: EdgeInsets.only(top: 20.0),
child: Form(
autovalidate: true,
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
LabelTextPadding(text: 'Business Information'),
//business name
RegularTextPadding(regText: 'Business Name'),
_buildBusinessName(),
//business type
RegularTextPadding(regText: 'Business Type'),
_buildBusinessType(),
//Trading currency
RegularTextPadding(regText: 'Trading Currency'),
_buildTradingCurrency(),
//business location
RegularTextPadding(regText: 'Location'),
//address 1
_buildAddress(),
//city
_buildCityField(),
//postcode
_buildPostcode(),
//state
_buildStateField(),
//country
_buildCountry(),
SizedBox(
height: 20.0,
),
DividerClass(),
SizedBox(
height: 20.0,
),
//Personal information
LabelTextPadding(
text: 'Personal Information',
),
_buildFirstNameField(),
_buildLastNameField(),
_buildPhoneNumberField(),
// _buildEmailField(),
_buildButtons(),
],
),
),
),
);
}
_buildBusinessName() {
if (_userData.businessName == null) {
return PaddedInputTextFormField(
hintText: 'update business name',
onSaved: (value) {
_userData.businessName = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.businessName,
onSaved: (value) {
_userData.businessName = value;
},
);
}
}
_buildBusinessType() {
return Padding(
padding: const EdgeInsets.all(20.0),
child: DropdownButton(
value: _userData.businessType == null
? selectedBusinessTypeDropDownValue
: _userData.businessType,
icon: Icon(FontAwesomeIcons.caretDown),
elevation: 15,
underline: Container(
height: 2,
color: kThemeStyleButtonFillColour,
),
items: businessType
.map(
(businessType) => DropdownMenuItem(
value: businessType, child: Text(businessType)),
)
.toList(),
onChanged: (newValue) {
setState(() {
selectedBusinessTypeDropDownValue = newValue;
_userData.businessType = newValue;
});
},
),
);
}
_buildTradingCurrency() {
return Padding(
padding: const EdgeInsets.all(20.0),
child: DropdownButton(
value: _userData.tradingCurrency == null
? selectedCurrencyDropDownValue
: _userData.tradingCurrency,
icon: Icon(FontAwesomeIcons.caretDown),
elevation: 15,
underline: Container(
height: 2,
color: kThemeStyleButtonFillColour,
),
items: tradingCurrency
.map(
(tradingCurrency) => DropdownMenuItem(
value: tradingCurrency, child: Text(tradingCurrency)),
)
.toList(),
onChanged: (newValue) {
setState(() {
selectedCurrencyDropDownValue = newValue;
_userData.tradingCurrency = newValue;
});
},
),
);
}
_buildAddress() {
if (_userData.streetAddress == null) {
return PaddedInputTextFormField(
hintText: 'address 1',
onSaved: (value) {
_userData.streetAddress = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.streetAddress,
onSaved: (value) {
_userData.streetAddress = value;
},
);
}
}
_buildCityField() {
if (_userData.city == null) {
return PaddedInputTextFormField(
hintText: 'update city',
onSaved: (value) {
_userData.city = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.city,
onSaved: (value) {
_userData.city = value;
},
);
}
}
_buildPostcode() {
if (_userData.postcode == null) {
return PaddedInputTextFormField(
hintText: 'update postcode',
onSaved: (value) {
_userData.postcode = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.postcode,
onSaved: (value) {
_userData.postcode = value;
},
);
}
}
_buildStateField() {
if (_userData.state == null) {
return PaddedInputTextFormField(
hintText: 'update state',
onSaved: (value) {
_userData.state = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.state,
onSaved: (value) {
_userData.state = value;
},
);
}
}
_buildCountry() {
return Padding(
padding: const EdgeInsets.all(20.0),
child: DropdownButton(
value: _userData.country == null
? selectedCountryDropDownValue
: _userData.country,
icon: Icon(FontAwesomeIcons.caretDown),
elevation: 15,
underline: Container(
height: 2,
color: kThemeStyleButtonFillColour,
),
items: country
.map(
(country) =>
DropdownMenuItem(value: country, child: Text(country)),
)
.toList(),
onChanged: (newValue) {
setState(() {
selectedCountryDropDownValue = newValue;
_userData.country = newValue;
});
},
),
);
}
//user personal info build
_buildFirstNameField() {
if (_userData.firstName == null) {
return PaddedInputTextFormField(
hintText: 'update first name',
onSaved: (value) {
_userData.firstName = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.firstName,
onSaved: (value) {
_userData.firstName = value;
},
);
}
}
_buildLastNameField() {
if (_userData.lastName == null) {
return PaddedInputTextFormField(
hintText: 'update last name',
onSaved: (value) {
_userData.lastName = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.lastName,
onSaved: (value) {
_userData.lastName = value;
},
);
}
}
_buildPhoneNumberField() {
if (_userData.phoneNumber == null) {
return PaddedInputTextFormField(
hintText: 'update phone number',
onSaved: (value) {
_userData.phoneNumber = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.lastName,
onSaved: (value) {
_userData.phoneNumber = value;
},
);
}
}
//build email form field
_buildEmailField() {
FutureBuilder<String>(
future: AuthProvider.of(context).auth.getCurrentUserEmail(),
// ignore: missing_return
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
return PaddedEmailInputTextField(
emailInput: snapshot.data,
onSaved: (value) => email = value,
);
},
);
}
_buildButtons() {
return Padding(
padding: const EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Buttons(
onPressedButton: () {
Navigator.pop(context);
},
buttonLabel: 'Cancel',
buttonColour: kThemeStyleButtonFillColour,
buttonTextStyle: kThemeStyleButton),
SizedBox(
width: 15.0,
),
Buttons(
onPressedButton: () => _saveUserData(context),
buttonLabel: 'Save',
buttonColour: kThemeStyleButtonFillColour,
buttonTextStyle: kThemeStyleButton),
],
),
);
}
}
class ProfileFormScreen扩展了StatefulWidget{
静态常量字符串id='profile_form';
@凌驾
_ProfileFormScreenState createState()=>\u ProfileFormScreenState();
}
类_ProfileFormScreenState扩展状态{
最终的GlobalKey _formKey=GlobalKey();
用户数据(UserData);;
//全球宣言
字符串selectedBusinessTypeDropDownValue='Fashion';
字符串selectedCurrencyDropDownValue='NGN:Nigerian Naira';
字符串selectedCountryDropDownValue='Nigeria';
字符串电子邮件;
_saveUserData(构建上下文){
UserDataNotifier用户通知程序=
Provider.of(上下文,listen:false);
如果(!\u formKey.currentState.validate()){
返回;
}
_formKey.currentState.save();
updateUserData(_userData,true);
Navigator.pop(上下文);
}
@凌驾
void initState(){
super.initState();
UserDataNotifier UserDataNotifier=
Provider.of(上下文,listen:false);
if(userDataNotifier.loggedInUserData!=null){
_userData=userDataNotifier.loggedInUserData;
}否则{
_userData=userData();
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“编辑配置文件”),
),
正文:SingleChildScrollView(
填充:仅限边缘设置(顶部:20.0),
孩子:表格(
自动验证:true,
键:_formKey,
子:列(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
LabelTextPadding(文本:“业务信息”),
//企业名称
RegularTextPadding(regText:“企业名称”),
_buildBusinessName(),
//业务类型
RegularTextPadding(regText:“业务类型”),
_buildBusinessType(),
//交易货币
RegularTextPadding(regText:“交易货币”),
_buildTradingCurrency(),
//营业地点
RegularTextPadding(regText:“位置”),
//地址1
_buildAddress(),
//城市
_buildCityField(),
//邮政编码
_buildPostcode(),
//陈述
_buildStateField(),
//国家
_buildCountry(),
大小盒子(
身高:20.0,
),
DividerClass(),
大小盒子(
身高:20.0,
),
//个人信息
LabelTextPadding(
文本:“个人信息”,
),
_buildFirstNameField(),
_buildLastNameField(),
_buildPhoneNumberField(),
//\u buildEmailField(),
_buildButtons(),
],
),
),
),
);
}
_buildBusinessName(){
if(_userData.businessName==null){
返回填充的PuttenTextFormField(
hintText:“更新企业名称”,
已保存:(值){
_userData.businessName=值;
},
);
}否则{
返回填充的PuttenTextFormField(
inputValue:_userData.businessName,
已保存:(值){
_userData.businessName=值;
},
);
}
}
_buildBusinessType(){
返回填充(
填充:常数边集全部(20.0),
孩子:下拉按钮(
值:_userData.businessType==null
?selectedBusinessTypeDropDownValue
:_userData.businessType,
图标:图标(FontAwesomeIcons.caretDown),
标高:15,
下划线:容器(
身高:2,
颜色:KThemestyleButtonFillColor,
),
项目:业务类型
.地图(
(业务类型)=>下拉菜单项(
值:businessType,子项:Text(businessType)),
)
.toList(),
一旦更改:(newValue){
设置状态(){
selectedBusinessTypeDropDownValue=newValue;
_userData.businessType=newValue;
});
},
),
);
}
_BuildingTradingCurrency(){
返回填充(
填充:常数边集全部(20.0),
孩子:下拉按钮(
值:_userData.tradingCurrency==null
?选择的电流下拉值
:_userData.tradingCurrency,
图标:图标(FontAwesomeIcons.caretDown),
标高:15,
下划线:容器(
身高:2,
颜色:KThemestyleButtonFillColor,
),
项目:贸易货币
.地图(
(交易货币)=>下拉菜单项(
值:tradingCurrency,子项:文本(tradingCurrency)),
)
.toList(),
一旦更改:(newValue){
设置状态(){
selectedCurrencyDropDownValue=newValue;
_userData.tradingCurrency=newValue;
});
},
),
);
}
_buildAddress(){
if(_userData.streetAddress==null){
返回填充的PuttenTextFormField(
hintText:'地址1',
已保存:(值){
_userData.streetAddress=值;
},
);
}否则{
返回填充的PuttenTextFormField(
inputValue:_userData.streetAddress,
已保存:(值){
_userData.streetAddress=值;
},
);
}
}
_buildCityField(){
如果(_userData.city==null){
返回填充的PuttenTextFormField(
hintText:“更新城市”,
已保存:(值){
_userData.city=值;
},
);
}否则{
返回paddedInputExtFo
class ProfileFormScreen extends StatefulWidget {
static const String id = 'profile_form';
@override
_ProfileFormScreenState createState() => _ProfileFormScreenState();
}
class _ProfileFormScreenState extends State<ProfileFormScreen> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
UserData _userData;
//global declarations
String selectedBusinessTypeDropDownValue = 'Fashion';
String selectedCurrencyDropDownValue = 'NGN: Nigerian Naira';
String selectedCountryDropDownValue = 'Nigeria';
String email;
_saveUserData(BuildContext context) {
UserDataNotifier userNotifier =
Provider.of<UserDataNotifier>(context, listen: false);
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
userNotifier.updateUserData(_userData, true);
Navigator.pop(context);
}
@override
void initState() {
super.initState();
UserDataNotifier userDataNotifier =
Provider.of<UserDataNotifier>(context, listen: false);
if (userDataNotifier.loggedInUserData != null) {
_userData = userDataNotifier.loggedInUserData;
} else {
_userData = UserData();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Profile'),
),
body: SingleChildScrollView(
padding: EdgeInsets.only(top: 20.0),
child: Form(
autovalidate: true,
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
LabelTextPadding(text: 'Business Information'),
//business name
RegularTextPadding(regText: 'Business Name'),
_buildBusinessName(),
//business type
RegularTextPadding(regText: 'Business Type'),
_buildBusinessType(),
//Trading currency
RegularTextPadding(regText: 'Trading Currency'),
_buildTradingCurrency(),
//business location
RegularTextPadding(regText: 'Location'),
//address 1
_buildAddress(),
//city
_buildCityField(),
//postcode
_buildPostcode(),
//state
_buildStateField(),
//country
_buildCountry(),
SizedBox(
height: 20.0,
),
DividerClass(),
SizedBox(
height: 20.0,
),
//Personal information
LabelTextPadding(
text: 'Personal Information',
),
_buildFirstNameField(),
_buildLastNameField(),
_buildPhoneNumberField(),
// _buildEmailField(),
_buildButtons(),
],
),
),
),
);
}
_buildBusinessName() {
if (_userData.businessName == null) {
return PaddedInputTextFormField(
hintText: 'update business name',
onSaved: (value) {
_userData.businessName = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.businessName,
onSaved: (value) {
_userData.businessName = value;
},
);
}
}
_buildBusinessType() {
return Padding(
padding: const EdgeInsets.all(20.0),
child: DropdownButton(
value: _userData.businessType == null
? selectedBusinessTypeDropDownValue
: _userData.businessType,
icon: Icon(FontAwesomeIcons.caretDown),
elevation: 15,
underline: Container(
height: 2,
color: kThemeStyleButtonFillColour,
),
items: businessType
.map(
(businessType) => DropdownMenuItem(
value: businessType, child: Text(businessType)),
)
.toList(),
onChanged: (newValue) {
setState(() {
selectedBusinessTypeDropDownValue = newValue;
_userData.businessType = newValue;
});
},
),
);
}
_buildTradingCurrency() {
return Padding(
padding: const EdgeInsets.all(20.0),
child: DropdownButton(
value: _userData.tradingCurrency == null
? selectedCurrencyDropDownValue
: _userData.tradingCurrency,
icon: Icon(FontAwesomeIcons.caretDown),
elevation: 15,
underline: Container(
height: 2,
color: kThemeStyleButtonFillColour,
),
items: tradingCurrency
.map(
(tradingCurrency) => DropdownMenuItem(
value: tradingCurrency, child: Text(tradingCurrency)),
)
.toList(),
onChanged: (newValue) {
setState(() {
selectedCurrencyDropDownValue = newValue;
_userData.tradingCurrency = newValue;
});
},
),
);
}
_buildAddress() {
if (_userData.streetAddress == null) {
return PaddedInputTextFormField(
hintText: 'address 1',
onSaved: (value) {
_userData.streetAddress = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.streetAddress,
onSaved: (value) {
_userData.streetAddress = value;
},
);
}
}
_buildCityField() {
if (_userData.city == null) {
return PaddedInputTextFormField(
hintText: 'update city',
onSaved: (value) {
_userData.city = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.city,
onSaved: (value) {
_userData.city = value;
},
);
}
}
_buildPostcode() {
if (_userData.postcode == null) {
return PaddedInputTextFormField(
hintText: 'update postcode',
onSaved: (value) {
_userData.postcode = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.postcode,
onSaved: (value) {
_userData.postcode = value;
},
);
}
}
_buildStateField() {
if (_userData.state == null) {
return PaddedInputTextFormField(
hintText: 'update state',
onSaved: (value) {
_userData.state = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.state,
onSaved: (value) {
_userData.state = value;
},
);
}
}
_buildCountry() {
return Padding(
padding: const EdgeInsets.all(20.0),
child: DropdownButton(
value: _userData.country == null
? selectedCountryDropDownValue
: _userData.country,
icon: Icon(FontAwesomeIcons.caretDown),
elevation: 15,
underline: Container(
height: 2,
color: kThemeStyleButtonFillColour,
),
items: country
.map(
(country) =>
DropdownMenuItem(value: country, child: Text(country)),
)
.toList(),
onChanged: (newValue) {
setState(() {
selectedCountryDropDownValue = newValue;
_userData.country = newValue;
});
},
),
);
}
//user personal info build
_buildFirstNameField() {
if (_userData.firstName == null) {
return PaddedInputTextFormField(
hintText: 'update first name',
onSaved: (value) {
_userData.firstName = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.firstName,
onSaved: (value) {
_userData.firstName = value;
},
);
}
}
_buildLastNameField() {
if (_userData.lastName == null) {
return PaddedInputTextFormField(
hintText: 'update last name',
onSaved: (value) {
_userData.lastName = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.lastName,
onSaved: (value) {
_userData.lastName = value;
},
);
}
}
_buildPhoneNumberField() {
if (_userData.phoneNumber == null) {
return PaddedInputTextFormField(
hintText: 'update phone number',
onSaved: (value) {
_userData.phoneNumber = value;
},
);
} else {
return PaddedInputTextFormField(
inputValue: _userData.lastName,
onSaved: (value) {
_userData.phoneNumber = value;
},
);
}
}
//build email form field
_buildEmailField() {
FutureBuilder<String>(
future: AuthProvider.of(context).auth.getCurrentUserEmail(),
// ignore: missing_return
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
return PaddedEmailInputTextField(
emailInput: snapshot.data,
onSaved: (value) => email = value,
);
},
);
}
_buildButtons() {
return Padding(
padding: const EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Buttons(
onPressedButton: () {
Navigator.pop(context);
},
buttonLabel: 'Cancel',
buttonColour: kThemeStyleButtonFillColour,
buttonTextStyle: kThemeStyleButton),
SizedBox(
width: 15.0,
),
Buttons(
onPressedButton: () => _saveUserData(context),
buttonLabel: 'Save',
buttonColour: kThemeStyleButtonFillColour,
buttonTextStyle: kThemeStyleButton),
],
),
);
}
}
documentReference.setData(userData.toMap())