【问题标题】:Flutter ChangeNotifierProvider issueFlutter ChangeNotifierProvider 问题
【发布时间】: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


    【解决方案1】:

    我们在 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。

    【讨论】:

    • 我在每次更改时更新 propertyVM,然后我有一个执行 saveProperty 的回调,并在其中执行 notifyListeners(),但它并没有显示更改方法中的值实际更新。
    猜你喜欢
    • 2020-05-10
    • 2020-09-21
    • 2019-11-15
    • 2020-11-20
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    相关资源
    最近更新 更多