Flutter/Dart:基于 AsyncValue 列表返回 ShowDialog 的函数返回类型

问题描述 投票:0回答:1

我想为我的应用程序创建一个函数,该函数通过异步列表来检查以前是否购买了某个商品,如果购买过,则显示一个对话框。由于 Asyncvalue.when() 具有数据、错误和加载参数,因此 showDialog 的 Future Type 会创建与 CircularProgressIndicator 或我可能显示的错误文本消息的返回类型冲突。你能告诉我他下面的方法是否错误,或者有什么建议吗?

Future<Void> itemPreviouslyBought(BuildContext context, Item newItem, AsyncValue<List<Item>> currentItemList) {
    return currentItemList.when(
        data: (item) {
          final matchedElement = item.firstWhere(
                (element) =>
            element.productName.toLowerCase() ==
                newItem.productName.toLowerCase(),
          );
          
          return showDialog<void>(
            context: context,
            barrierDismissible: false,
            builder: (BuildContext context) {
              return AlertDialog(
                title: const Text('Item Previously Bought!'),
                content: SingleChildScrollView(
                  child: ListBody(
                    children: <Widget>[
                      Text("You have bought this item before."),
                    ],
                  ),
                ),
                actions: <Widget>[
                  TextButton(
                    child: const Text('Okay'),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            },
          );
        },
      loading: () => const Center(child: CircularProgressIndicator()),
      error: (e, st) => Center(child: Text(e.toString())),
    );
  }

错误消息如下

Center widget isn't returnable from Future<InvalidType> function.

android flutter dart async-await android-widget
1个回答
0
投票
Future<void> itemPreviouslyBought(BuildContext context, Item newItem, AsyncValue<List<Item>> currentItemList) async {
  return currentItemList.when(
    data: (items) async {
      final matchedElement = items.firstWhere(
        (element) => element.productName.toLowerCase() == newItem.productName.toLowerCase(),
        orElse: () => null,
      );

      if (matchedElement != null) {
        await showDialog<void>(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            return AlertDialog(
              title: const Text('Item Previously Bought!'),
              content: SingleChildScrollView(
                child: ListBody(
                  children: <Widget>[
                    Text("You have bought this item before."),
                  ],
                ),
              ),
              actions: <Widget>[
                TextButton(
                  child: const Text('Okay'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            );
          },
        );
      }
    },
    loading: () async {
      // Handle loading state if needed
    },
    error: (e, st) async {
      // Handle error state if needed
    },
  );
}

AsyncValue.when 方法期望所有分支返回相同的类型,但您将 Future 与 Center 和 Text 等小部件混合在一起

我希望这可以解决您的问题,

© www.soinside.com 2019 - 2024. All rights reserved.