信息

发布信息

发布
显示总共113条中的前3条
flutter error A ProductProvider was used after being disposed.
发布者 : 粮油菜大市场 发布时间 : 25 Nov 2021 07:04:55

A ProductProvider was used after being disposed. 

可能是 provider的一个子节点,已经声明过父节点了,改成value的形式

ChangeNotifierProvider.value(
                    value: ProductProvider.instance(false)),

 

There should be exactly one item with [DropdownButton]'s value: disabled
发布者 : 粮油菜大市场 发布时间 : 25 Nov 2021 07:02:45

There should be exactly one item with [DropdownButton]'s value: disabled. 

Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

'package:flutter/src/material/dropdown.dart':

package:flutter/…/material/dropdown.dart:1

 

flutter checkbox can't update status
发布者 : 粮油菜大市场 发布时间 : 25 Nov 2021 02:57:34
Widget _buildCheckBox(_product) {
    bool? _productStatus;
    if (_productStatus == null) {
      _productStatus = _product.status == 'enabled' ? true : false;
    }
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
      return Checkbox(
          value: _productStatus,
          activeColor: Theme.of(context).primaryColor,
          onChanged: (_value) async {
            if (_value != null) {
              setState(() {
                _productStatus = _value;
              });
              Map<String, dynamic> _params = {
                'product_id': _product.id,
                'merchant_id': _product.merchantId,
                'status': _value ? 'enabled' : 'disabled',
              };
              await Provider.of(context, listen: false)
                  .updateProductStatus(_params)
                  .then((_) {
                if (Provider.of(context, listen: false)
                        .errorMsg ==
                    null) {
                  String _result = _value ? '上线' : '下线';
                  ScaffoldMessenger.of(context)
                      .showSnackBar(SnackBar(content: Text('产品已经$_result')));
                } else {
                  ScaffoldMessenger.of(context)
                      .showSnackBar(SnackBar(content: Text('产品状态更新失败')));
                }
              });
            }
          });
    });
}
 
这段代码开始checkbox 不更新选中状态,网上搜素了一下,
在外面包了
StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {})
 
开始的时候可以更新,等执行完毕
Provider
又把状态更新了,setState的状态又丢失了,