Flutter 中 ValueNotifier<List<T>> 監(jiān)聽(tīng)問(wèn)題解決

1. 起因

開(kāi)發(fā)中遇到一個(gè)問(wèn)題 ValueNotifier<List<T>> 監(jiān)聽(tīng)失敗, 初步確認(rèn)原因是數(shù)組值發(fā)生改變但是地址未發(fā)生改變,與 iOS 監(jiān)聽(tīng)數(shù)組需要特別處理一樣;需要二次賦值觸發(fā)地址改變,觸發(fā)監(jiān)聽(tīng)機(jī)制;

2.結(jié)果:完美解決問(wèn)題

最終簡(jiǎn)單封裝如下:

/// 泛型數(shù)組監(jiān)聽(tīng)
class ValueNotifierList<T> extends ValueNotifier<List<T>> {

  ValueNotifierList(List<T> initValue) : super(initValue);

  void add(T item) {
    value.add(item);
    _copyValue();
  }

  /// 刪除
  void remove(int index) {
    if (value.length < index) {
      return;
    }
    value.removeAt(index);
    _copyValue();
  }

  /// 刪除最后
  void removeLast() {
    if (value.length == 0) {
      return;
    }
    value.removeLast();
    _copyValue();
  }

  void removeAll() {
    value.clear();
    _copyValue();
  }
  /// 利用深copy 重新賦值,觸發(fā)監(jiān)聽(tīng)
  void _copyValue() {
    value = [...value];
  }

  @override
  String toString() {
    return "${this.runtimeType} 共 ${value.length} 件商品}";
  }
}

3. example:

/// ValueNotifier
static ValueNotifierList valueNotifierList = ValueNotifierList(<OrderModel>[]);
/// ValueNotifier(addListener無(wú)效 因?yàn)閿?shù)組地址未發(fā)生改變, 推薦使用 ValueNotifierList)
static ValueNotifier<List<OrderModel>> valueNotifierListOrigin = ValueNotifier(<OrderModel>[]);

...

@override
void initState() {
  super.initState();
  
  valueNotifierList.addListener(update);
  valueNotifierListOrigin.addListener(update);
}

@override
void dispose() {
  valueNotifierList.removeListener(update);
  valueNotifierListOrigin.removeListener(update);
  
  super.dispose();
}

...

void update() {
  showSnackBar(SnackBar(content: Text("數(shù)據(jù)變化監(jiān)聽(tīng)回調(diào), 刷新重建界面",)), true);
  setState(() {});
}

...

void handleActionNum({required ValueNotifierModel e, required int value, required int idx}) {
  switch (e.name) {
    case "valueNotifierList":
      {
        final e = OrderModel(name: '商品', id: 99, pirce: 1.00);
        if (value > 0) {
          valueNotifierList.add(e);
        } else {
          valueNotifierList.removeLast();
        }

        ddlog(valueNotifierList.toString());
        // ddlog("${cartModelOneKey.totalPrice}");
      }
      break;

    case "valueNotifierListOrigin":
      {
        final e = OrderModel(name: '商品', id: 99, pirce: 1.00);
        if (value > 0) {
          valueNotifierListOrigin.value.add(e);
        } else {
          valueNotifierListOrigin.value.removeLast();
        }
        
        update();///監(jiān)聽(tīng)無(wú)效,需要手動(dòng)調(diào)整

        ddlog(valueNotifierListOrigin.value.length.toString());
        // ddlog("${cartModelOneKey.totalPrice}");
      }
      break;
    default:
      break;
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容