Flutter Provider中带有回调函数的ChangeNotifier类不起作用。

huangapple go评论119阅读模式
英文:

flutter provider ChangeNotifier class with a call-back function not working

问题

I'm using provider and my code looks like this.

class AbcClass with ChangeNotifier {
  String abc1 = '111';
  String abc2 = '222';

  List<void Function(String)> get abcInputs {
    return [
      (newValue) {
        abc1 = newValue;
        notifyListeners();
      },
      (newValue) {
        abc2 = newValue;
        notifyListeners();
      }
    ];
  }
}

and I'm setting abcInputs (callback function) to UI textfield.

Widget build(BuildContext context) {
  context.watch<AbcClass>();
  AbcClass abcClass = context.read<AbcClass>();

  return TextFormField(
    onChanged: abcClass.abcInputs[0],
    initialValue: abcClass.abc1.toString(),
  );
}

My problem is when I change the input value from UI, abcClass, abc1 value does not change. Can anyone help with that? Appreciate your help.

英文:

I'm using provider and my code looks like this.

class AbcClass with ChangeNotifier {


String abc1=&#39;111&#39;;
String abc2=&#39;222&#39;;



List&lt;void Function(String)&gt; get abcInputs {
    return [
      (newValue) =&gt; () {
            abc1 = newValue;
            notifyListeners();
          },
      (newValue) =&gt; () {
            abc2 = newValue;
            notifyListeners();
          }
    ];
  }


}

and i'm setting abcInputs(callback function) to UI textfield.

Widget build(BuildContext context) {

context.watch&lt;AbcClass&gt;();
AbcClass abcClass= context.read&lt;AbcClass&gt;();
               

return TextFormField(
onChanged: abcClass.abcInputs[0],
initialValue:abcClass.abc1.toString(),

}

My problem is when i changed the input value from UI,
abcClass , abc1 value not change.
can anyone help to do that.
appreciate your help.

答案1

得分: 0

问题是 ' => ' ,当我移除它时,它能工作。

List<void Function(String)> get abcInputs {
    return [
        (newValue) () {
            abc1 = newValue;
            notifyListeners();
        },
        (newValue) () {
            abc2 = newValue;
            notifyListeners();
        }
    ];
}
英文:

Problem was ' => ' , when I removed it. it works

List&lt;void Function(String)&gt; get abcInputs {
    return [
      (newValue) =&gt; () {
            abc1 = newValue;
            notifyListeners();
          },
      (newValue) =&gt; () {
            abc2 = newValue;
            notifyListeners();
          }
    ];
  }

to this

   List&lt;void Function(String)&gt; get abcInputs {
        return [
          (newValue) () {
                abc1 = newValue;
                notifyListeners();
              },
          (newValue) () {
                abc2 = newValue;
                notifyListeners();
              }
        ];
      }

huangapple
  • 本文由 发表于 2023年6月26日 20:12:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定