无法侦听flutter riverpod中的状态更改

cetgtptt  于 2022-12-27  发布在  Flutter
关注(0)|答案(1)|浏览(165)

当我更改一个文件(dropdown.dart)中的下拉值时,我希望访问另一个文件total.dart中的更改值。
Dropdown.dart

Expanded dropDown(WidgetRef ref, String id) {
  var items = ref.watch(TripProvider.listItemProvider);
  return Expanded(
    child: DropdownButtonFormField(
      hint: const Text('Select items'),
      items: items.map<DropdownMenuItem<String>>((Map<String, dynamic> res) {
        return DropdownMenuItem<String>(
          value: res['id'],
          child: Text(res['item']),
        );
      }).toList(),
      onChanged: (val) {
        TripProvider.onDropDownChange(
          val!,
          ref,
          id,
        );
      },
    ),
  );
}

Total.dart

class TotalData extends StatelessWidget {
  const TotalData({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (_, ref, __) {
      var arrayItem = ref.watch(TripProvider.listProvider);
      print("arrayItem: $arrayItem");
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: const <Text>[
          Text('Unit price: Rs. 500'),
          Text('Total: Rs.1950'),
        ],
      );
    });
  }
}

和提供程序文件

static var onDropDownChange =
      (String value, WidgetRef ref, String itemArrayId) {
    var oldState = ref.read(TripProvider.listProvider);
    var items = ref.read(TripProvider.listItemProvider);
    for (var i = 0; i < oldState.length; i++) {
      if (oldState[i]['id'] == itemArrayId) {
        oldState[i]['selectedItem'] = value;
      }
      for (var j = 0; j < items.length; j++) {
        if (items[j]['id'] == oldState[i]['selectedItem']) {
          oldState[i]['unitPrice'] = items[j]['price'];
        }
      }
    }
    ref.read(TripProvider.listProvider.notifier).update((state) => oldState);
  };

我想访问total.dart中的状态更改。我是否遗漏了什么?我无法侦听total.dart文件中提供程序文件中发生的更改。

ftf50wuq

ftf50wuq1#

由于您还没有提供完整的TripProvider,让我向您展示一种使用riverpod监听状态更改的简单方法。

// create a provider that stores string value (like your dropdown value)
final selectedTripProvider = StateProvider<String>((ref) => "");

Dropdown.dart

Expanded dropDown(WidgetRef ref, String id) {
  var items = ref.watch(TripProvider.listItemProvider);
  return Expanded(
    child: DropdownButtonFormField(
      hint: const Text('Select items'),
      items: items.map<DropdownMenuItem<String>>((Map<String, dynamic> res) {
        return DropdownMenuItem<String>(
          value: res['id'],
          child: Text(res['item']),
        );
      }).toList(),
      onChanged: (val) {
        // look here
        ref.read(selectedTripProvider.notifier).state = val ?? "";
      },
    ),
  );
}

Total.dart

class TotalData extends StatelessWidget {
  const TotalData({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (_, ref, __) {
      // look Here
      final selectedTrip= ref.watch(selectedTripProvider);
      print("selectedTrip: $selectedTrip");
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: const <Text>[
          Text('Unit price: Rs. 500'),
          Text('Total: Rs.1950'),
        ],
      );
    });
  }
}

相关问题