StreamProvider传递值并更新流结果

时间:2020-04-28 14:51:30

标签: flutter dart provider

一旦应用加载更改StreamProvider的值不会更改Stream输出/数据,我试图将变量传递给endTime

class _PrayersGlobalState extends State<PrayersGlobal> {
  PrayersViewModel modelGlobal = serviceLocator<PrayersViewModel>();

  @override
  Widget build(BuildContext context) {
    modelGlobal.streamData(
      prayerType: 1,
      userId: 1,
      updatedOn: '2020-04-10',
      endTime: '2020-05-' + ((1 + _random.nextInt(5)) * 5).toString(),
    );
    return MultiProvider(
      providers: [
        StreamProvider<List<Prayers>>(
          create: (context) => modelGlobal.streamPrayers,
          catchError: (context, err) {
            print('Error : ' + err.toString());
            return;
          },
        ),
      ],

这是 streamData 函数,它从GraphQL订阅获取流

void streamData({
    int prayerType,
    int userId,
    String updatedOn,
    String endTime,
  }) {
    final Stream<Prayers> streamPs = _graphQLService.getPrayerStream(
      prayerType: prayerType,
      userId: userId,
      updatedOn: updatedOn,
      endTime: endTime,
    );
    print('EndTime In Model : $endTime');
    this._streamPrayers.sink.addStream(streamPs);
  }

1 个答案:

答案 0 :(得分:1)

我正在使用名为hasura_connect的flutter程序包,此代码可以100%正确地工作

https://pub.dev/packages/hasura_connect

我没有正确阅读文档,有一个单独的函数需要调用以将不同的值传递给变量。

//change values of variables for PAGINATIONS
snapshot.changeVariable({"limit": 20});