如何在flutter Hooks useFuture()中使用多个嵌套的异步调用?

时间:2021-01-26 19:57:26

标签: flutter flutter-hooks

我有一个 flutter hook,它从本地存储中获取 currentSettings,并根据 localstorage 中存在的值生成一些设置。

ValueNotifier<Map<String, dynamic>> useCurrentSettings() {
  final currentSettings = useValueNotifier({
    "currentFeeling": {},
    "color": null,
    "currentLayout": "modern",
  });

在这里,从 localstorage 获取所有可用的设置,

  final currentFeeling = useFuture(useMemoized(() => "currentFeeling".local));
  final currentLayout = useFuture(useMemoized(() => "currentLayout".local));
  var color;

一旦来自 localstorage 的 currentFeeling 数据可用,我想根据 currentFeeling 中存在的 imageUrl 找到匹配的调色板,

  if (currentFeeling.hasData) {

    final parsedFeeling = json.decode(currentFeeling.data);
    currentSettings.value.update('currentFeeling', (_) => parsedFeeling);

    if (color == null) {
      color = useFuture(useMemoized(() => getColorPalette(parsedFeeling["imageUrl"])));
    } else if (color.hasData) {
      currentSettings.value.update('color', (_) => color);
    }

  }

  if (currentLayout.hasData) {
    currentSettings.value.update('currentLayout', (_) => currentLayout.data);
  }
  

  return currentSettings;
}

getColorPalette 如下,从网络动态获取图像并找到匹配的调色板:

getColorPalette(feeling) async {
  final PaletteGenerator generator = await PaletteGenerator.fromImageProvider(
    NetworkImage(feeling["url"]),
    maximumColorCount: 20,
  );
  return generator;
}

我在尝试使用这个自定义钩子时遇到如下不匹配钩子:

Bad state: Type mismatch between hooks:
- previous hook: _ListenableHook
- new hook: _MemoizedHook<Future<dynamic>>

有什么指点吗?

0 个答案:

没有答案