虽然刷新了provider,但Flutter Hooks Riverpod未更新小部件


我已经学习flutter几个月了,现在我正在试验Hooks和Riverpod,这将是非常重要的,所以一些结果可以被提供者缓存并重用,只有在有更新时才能真正重新获取。

但我在这里遇到了一个问题,我无法理解提供程序更新以反映在Widget中。完整的示例可以从这里检出,->,https://github.com/codespair/riverpod_update_issue,我已经添加了一些调试打印,我可以看到提供程序已经正确刷新,但是更改没有反映在小部件上。

该示例有一个有效的示例提供程序:

// create simple FutureProvider with respective future call next
final futureListProvider =
    FutureProvider.family<List<String>, int>((ref, value) => _getList(value));

// in a real case there would be an await call inside this function to network or local db or file system, etc...
Future<List<String>> _getList(int value) async {
  List<String> result = [...validValues];
  if (value == -1) {
    // do nothing just return original result...
  } else {
    result = []..add(result[value]);
  }
  debugPrint('Provider refreshed, result => $result');
  return result;
}

更改后的下拉列表将刷新提供程序:

             Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.fromLTRB(5, 2, 5, 1),
                  child: DropdownButton<String>(
                    key: UniqueKey(),
                    value: dropDownValue.value.toString(),
                    icon: Icon(Icons.arrow_drop_down),
                    iconSize: 24,
                    elevation: 16,
                    underline: Container(
                      height: 1,
                      color: Theme.of(context).primaryColor,
                    ),
                    onChanged: (String? newValue) {
                      dropDownValue.value = newValue!;
                      context
                          .refresh(futureListProvider(intFromString(newValue)));
                    },
                    items: validValues
                        .map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,
                        child: Text(
                          value,
                          style: Theme.of(context).primaryTextTheme.subtitle1,
                        ),
                      );
                    }).toList(),
                  ),
                ),

还有一个简单的列表,它使用provider元素来呈现,尽管provider被正确地刷新了,正如您在debugPrinting中看到的那样,它从不更新:

Container(
          key: UniqueKey(),
          height: 200,
          child: stringListProvider.when(
            data: (stringList) {
              debugPrint('List from Provider.when $stringList');
              return MyListWidget(stringList);
              // return _buildList(stringList);
            },
            loading: () => CircularProgressIndicator(),
            error: (_, __) => Text('OOOPsss error'),
          ),
        ),
      ]),


class MyListWidget extends HookWidget {
  final GlobalKey<ScaffoldState> _widgetKey = GlobalKey<ScaffoldState>();
  final List<String> stringList;

  MyListWidget(this.stringList);

  @override
  Widget build(BuildContext context) {
    debugPrint('stringList in MyListWidget.build $stringList');
    return ListView.builder(
      key: _widgetKey,
      itemCount: stringList.length,
      itemBuilder: (BuildContext context, int index) {
        return Card(
          key: UniqueKey(),
          child: Padding(
              padding: EdgeInsets.all(10), child: Text(stringList[index])),
        );
      },
    );
  }

当我评估一些开发应用程序的方法时,我倾向于采用更直接的方法来处理这些情况,因此我也愿意评估更简单、更直接的方法,但我真的很喜欢hooks_riverpod的useMemoized、useState等一些功能。

转载请注明出处:http://www.souyuntu.com/article/20230526/1265927.html