【发布时间】:2021-06-06 18:26:25
【问题描述】:
我有一个 ProperyListPage 并且在每个列表项上单击这是它所做的操作。
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChangeNotifierProvider.value(
value: property,
child: PropertyPage(),
),
),
);
}
在PropertyPage里面我有BottomNavigationBar,当你点击EditBottomNavigationBarItem然后它会进入一个像这样的PropertyEditTab
PropertyEditTab(),
在PropertyEditTab里面我有一个像这样的表单小部件
child: Form(
key: _formKey,
child: Column(
children: [
PropertyForm(
callback: saveProperty,
),
...
)...
我的 saveProperty 回调看起来像这样
void saveProperty(BuildContext context) async {
if (_formKey.currentState.validate()) {
PropertyViewModel _propertyVM = Provider.of<PropertyViewModel>(context, listen: false);
savingProperty();
final isSaved = await _propertyVM.updateProperty();
if (isSaved) {
propertySaved();
} else {}
}
}
updateProperty 方法会像这样对 Firestore 进行更新
class PropertyViewModel extends ChangeNotifier {
...
Future<bool> updateProperty() async {
bool isSaved = false;
User user = _firebaseAuth.currentUser;
print(uid);
final property = Property(
user.email,
...
);
try {
await _firestore
.collection(Constants.propertyCollection)
.doc(property.uid)
.update(property.toMap());
isSaved = true;
} on Exception catch (e) {
print(e);
message = 'Property not saved.';
} catch (e) {
print(e);
message = 'Error occurred';
}
notifyListeners();
return isSaved;
}
...
}
那么我的每一个表单域都是这样的
FormTextField(
name: 'address_line_1',
hintText: 'Address Line 1',
initialValue: propertyVM.addressLine1 ?? '',
onChanged: (value) {
propertyVM.addressLine1 = value;
},
),
这是我的 onPress 按钮来保存属性
ThemeButton(
text: 'Save Property',
buttonColor: Constants.kPrimaryColor,
textColor: Colors.white,
onPress: () => widget.callback(context),
),
问题是调用 notifyProviders() 时原始 PropertyViewModel 属性 没有更新,我不知道自己做错了什么。
提前感谢您的帮助。
【问题讨论】:
标签: flutter dart flutter-change-notifier changenotifier