Flutter Hook的useState与下拉框不起作用。

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

Flutter Hook useState not working with dropdown

问题

在选择下拉菜单的值后,useState中的值未更新。

这是我的模型类:

  1. class Data {
  2. final int id;
  3. final String name;
  4. final String someOtherData;
  5. Data({
  6. required this.id,
  7. required this.name,
  8. required this.someOtherData,
  9. });
  10. }

这是我的HookWidget:

  1. class Demo extends HookWidget {
  2. Demo({super.key});
  3. final data = [
  4. Data(id: 0, name: 'please pick one', someOtherData: 'no value'),
  5. Data(id: 10, name: 'ten', someOtherData: 'tenten'),
  6. Data(id: 20, name: 'twenty', someOtherData: 'twentytwenty'),
  7. ];
  8. @override
  9. Widget build(BuildContext context) {
  10. final selectedIndex = useState(0);
  11. final selectedData = useState(
  12. data.where((element) => element.id == selectedIndex.value).first,
  13. );
  14. return Column(
  15. mainAxisAlignment: MainAxisAlignment.start,
  16. mainAxisSize: MainAxisSize.min,
  17. children: [
  18. DropdownButton(
  19. value: selectedIndex.value,
  20. items: data
  21. .map(
  22. (e) => DropdownMenuItem(
  23. value: e.id,
  24. child: Text(
  25. e.name,
  26. ),
  27. ),
  28. )
  29. .toList(),
  30. onChanged: (value) => selectedIndex.value = value!,
  31. ),
  32. Text(
  33. selectedData.value.someOtherData,
  34. ),
  35. ],
  36. );
  37. }
  38. }

selectedIndex的值正常工作,没有任何问题。问题出在selectedData,在更新下拉菜单后:

  1. onChanged: (value) => selectedIndex.value = value!,

selectedIndex的值改变了,但selectedData的值一点也没变。请帮忙解决一下...

英文:

After choosing a value from dropdown, the value in useState does not update.

This is my model class

  1. class Data {
  2. final int id;
  3. final String name;
  4. final String someOtherData;
  5. Data({
  6. required this.id,
  7. required this.name,
  8. required this.someOtherData,
  9. });

}

This is my HookWidget

  1. class Demo extends HookWidget {
  2. Demo({super.key});
  3. final data = [
  4. Data(id: 0, name: 'please pick one', someOtherData: 'no value'),
  5. Data(id: 10, name: 'ten', someOtherData: 'tenten'),
  6. Data(id: 20, name: 'twenty', someOtherData: 'twentytwenty'),
  7. ];
  8. @override
  9. Widget build(BuildContext context) {
  10. final selectedIndex = useState(0);
  11. final selectedData = useState(
  12. data.where((element) => element.id == selectedIndex.value).first,
  13. );
  14. return Column(
  15. mainAxisAlignment: MainAxisAlignment.start,
  16. mainAxisSize: MainAxisSize.min,
  17. children: [
  18. DropdownButton(
  19. value: selectedIndex.value,
  20. items: data
  21. .map(
  22. (e) => DropdownMenuItem(
  23. value: e.id,
  24. child: Text(
  25. e.name,
  26. ),
  27. ),
  28. )
  29. .toList(),
  30. onChanged: (value) => selectedIndex.value = value!,
  31. ),
  32. Text(
  33. selectedData.value.someOtherData,
  34. ),
  35. ],
  36. );
  37. }
  38. }

Value of selectedIndex works fine, no problem at all. The problem is with selectedData, after updating the dropdown

  1. onChanged: (value) => selectedIndex.value = value!,

selectedIndex value changed, but selectedData value doesn't change at all. Help please...

答案1

得分: 2

为什么你认为它必须改变?useState(<initialData>) 中的 initialData 在第一次调用时初始化了 ValueNotifier,并在后续调用中被忽略。换句话说,selectedData 的值不会自动订阅到 selectedIndex 的值 - 它们是独立的 ValueNotifier。

如果你想缓存一个值并基于其他值(依赖项)重新计算它,请使用 useMemoized 钩子,像这样:

  1. final selectedIndex = useState(0);
  2. final selectedData = useMemoized(
  3. () => data[selectedIndex.value],
  4. [selectedIndex.value]
  5. );
英文:

Why do you think it must change? The initialData in useState(<initialData>) initializes the ValueNotifier on the first call and is ignored on subsequent calls. In other words, the value of selectedData is not automatically subscribed to the value of selectedIndex – they are separate ValueNotifier-s.

If you want to cache a value and recalculate it based on some other values (dependencies) use the useMemoized hook instead, like so:

  1. final selectedIndex = useState(0);
  2. final selectedData = useMemoized(
  3. () => data[selectedIndex.value],
  4. [selectedIndex.value]
  5. ),

huangapple
  • 本文由 发表于 2023年7月17日 13:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701811.html
匿名

发表评论

匿名网友

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

确定