1

I have a ProperyListPage and on each list item clicked this is the action it does.

    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider.value(
          value: property,
          child: PropertyPage(),
        ),
      ),
    );
  }

inside of PropertyPage I have BottomNavigationBar, when you click on Edit BottomNavigationBarItem then it goes to a PropertyEditTab like this

PropertyEditTab(),

inside of PropertyEditTab I have a Form widget like this

child: Form(
    key: _formKey,
    child: Column(
    children: [
    PropertyForm(
      callback: saveProperty,
    ),
   ...
)...

My saveProperty callback looks like this

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 {}
    }
}

the updateProperty method does an update to Firestore like this

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;
  }
...
}

then each one of my form fields look like this

FormTextField(
    name: 'address_line_1',
    hintText: 'Address Line 1',
    initialValue: propertyVM.addressLine1 ?? '',
    onChanged: (value) {
        propertyVM.addressLine1 = value;
    },
),

and this is my onPress of button to save property

ThemeButton(
    text: 'Save Property',
    buttonColor: Constants.kPrimaryColor,
    textColor: Colors.white,
    onPress: () => widget.callback(context),
),

the issue is that the original PropertyViewModel property isn't updating when notifyProviders() is called and I can't figure out what I am doing wrong.

Thanks in advance for the help.

4

1 回答 1

0

我们在 slack 频道上写作。

我认为原因是因为在FormTextField小部件中将其更改为:

FormTextField(
    name: 'address_line_1',
    hintText: 'Address Line 1',
    initialValue: propertyVM.addressLine1 ?? '',
    onChanged: (value) {
        propertyVM.updateAdressLine(value);
    },
),

在你PropertyViewModel做一个功能

void updateAdressLine() {
    adressLine1 = value;
    notifyListeners();
}

这是因为从您的 FormTextField 中您只是在更改值。您需要将更改打包到一种方法中,以便您可以一次更改值和 notifyListeners。

于 2021-03-08T21:58:39.717 回答