Provider ValueNotifier 监听在 Flutter 上的某些页面上不起作用

Provider ValueNotifier listen not working on some Pages on Flutter

listen 无效。更新热重载值时。

A页

  @override
  Widget build(BuildContext context) {

    ValueNotifier<List<ProductModel>> selectNotifier = Provider.of<ValueNotifier<List<ProductModel>>>(context, listen: true);

小部件

 Text('${selectNotifier.value.length}'),

B页

 InkWell(
     onTap: () {
         selectNotifier.value.add(selectProduct);                        
     },

main.dart

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<ValueNotifier<List<ProductModel>>>(
          create: (_) => ValueNotifier<List<ProductModel>>([]),
        ),
      ],
      child: MaterialApp(
        theme: CustomTheme.themeData,
        onGenerateRoute: Router.generateRoute,
        debugShowCheckedModeBanner: false,
      ),
    );
  }

版本

provider: ^4.1.2

Flutter 版本

1.17.2

我尝试了以下方法来解决这个问题。但我不知道什么是正确的方法(最好的方法)。

第一种方式

降级 Flutter 和 Provider 后,现在可以正常使用了。这是为什么?

provider: 3.2.0
git checkout v1.12.13-hotfixes

第二种方式

或者它也以这种方式工作。//但在 IDE

上发出警告
onTap: () {
             selectNotifier.value.add(selectProduct); 
             selectNotifier.notifyListeners(); //info: The member 'notifyListeners' can only be used within instance members of subclasses of 'package:flutter/src/foundation/change_notifier.dart'.                      
         },

但是警告消失了,添加这个ChangeNotifier,

class _viewState extends State<View> with ChangeNotifier{

添加 ChangeNotifier

后也出现错误

The following assertion was thrown while finalizing the widget tree: _CartItemViewState.dispose failed to call super.dispose.

dispose() implementations must always call their superclass dispose() method, to ensure that all the resources used by the widget are fully released.

第三种方式

我在这种方式上没有遇到任何问题,但我在生产应用程序中使用了太多 ValueNotifier,因此,其他的不是列表。我不知道如何更改其他类型。

 onTap: () {
        selectNotifier.value = List.from(selectNotifier.value)..add(widget.productModel);
}

在这道题中,第三种方式是正确的方式。

onTap: () {
        selectNotifier.value = List.from(selectNotifier.value)..add(widget.productModel);
}