带有提供者通知的表单内的 Flutter Checkbox 不起作用

Flutter Checkbox inside a form with Provider notification is not working

我有一个以表单形式呈现的复选框(未定义为 FormField,因为它不存在于 Flutter 中,并且尝试扩展它无论如何都不起作用):

import 'package:flutter/material.dart';
import 'checkbox_form_field.dart';
import 'package:sellertools/providers/money.dart';
import 'package:provider/provider.dart';


class MoneyFormCheckbox<T extends Money> extends StatelessWidget {
  final String label;

  const MoneyFormCheckbox(this.label, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final initValue = context.read<T>().money == 1;
    return Padding(
        padding: const EdgeInsets.symmetric(vertical: 10),
        child : Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child:
                  Checkbox(
                   value: initValue,
                   onChanged: (value) => context.read<T>().set(value! ? 1 : 0),
                  )
            )])
    );
  }

}

其中 Money class 是:

import 'package:flutter/foundation.dart';

class Money with ChangeNotifier {
  double _money = 0;

  double get money => _money;

  void set(double money) {
    _money = money;
    notifyListeners();
  }
}

这里的问题是,当我按下复选框时,没有任何反应,它保持未选中状态。同样的行为也适用于 TextFormField classes。知道如何使复选框也与表单内的 Provider 模式一起工作吗?

您应该将阅读更改为观看以订阅提供商:

final initValue = context.read<T>().money == 1;

final initValue = context.watch<T>().money == 1;