Flutter ChangeNotifierProvider issue

394 Views Asked by At

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.

1

There are 1 best solutions below

1
On

we were writing on the slack channel.

I think the reason is because in the FormTextField widget change it to this:

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

And in your PropertyViewModel make a function

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

It's because from your FormTextField you are only changing the value. You need pack the changes in a method so you can change the value and notifyListeners at one time.