使用变量名称将DART中的参数从一个匿名函数传递到另一个匿名函数

发布于 2025-01-24 07:15:27 字数 1852 浏览 2 评论 0原文

I'm reposting the (very slightly modified) code of a reader question from

void main() {
  double taxRate = .0825;
  double costOfProduct = 9.99;

  Function totalCostOfProduct =
      ({required double taxRate, required double costOfProduct}) {
    return (costOfProduct * taxRate) + costOfProduct;
  };

  print(totalCostOfProduct(taxRate: taxRate, costOfProduct: costOfProduct));

  String tellMeThePrice({required Function totalCostOfProduct}) {
    return "THE PRICE IS ${totalCostOfProduct}";
  }

  print(
    tellMeThePrice(
      totalCostOfProduct: totalCostOfProduct(
        taxRate: taxRate,
        totalCostOfProduct: costOfProduct,
      ),
    ),
  );
}

在dartpad中运行此问题给出以下错误:

TypeError: totalCostOfProduct.call$2$taxRate$totalCostOfProduct is not a function
Error: TypeError: totalCostOfProduct.call$2$taxRate$totalCostOfProduct is not a function

我认为这里有一些事情发生:

  • proting in in nellmetheprice尝试打印功能本身而不打印值,而无需打印值功能。
  • tellmetheprice实际上调用时,代码在函数(double)而不是函数本身的结果中传递。 (因此,类型错误。)

为了解决这些问题,我尝试了以下修改:

  String tellMeThePrice({required Function totalCostOfProduct}) {
    final result = totalCostOfProduct.call();
    return "THE PRICE IS $result";
  }

  print(
    tellMeThePrice(
      totalCostOfProduct: (taxRate, costOfProduct) => totalCostOfProduct(
        taxRate: taxRate,
        totalCostOfProduct: costOfProduct,
      ),
    ),
  );

这仍然返回一个错误:

TypeError: totalCostOfProduct.call$0 is not a function
Error: TypeError: totalCostOfProduct.call$0 is not a function

这是读者的一个人为示例使用两个函数。但是,这也使我陷入困境。如何将参数传递到第二个功能?

I'm reposting the (very slightly modified) code of a reader question from here.

void main() {
  double taxRate = .0825;
  double costOfProduct = 9.99;

  Function totalCostOfProduct =
      ({required double taxRate, required double costOfProduct}) {
    return (costOfProduct * taxRate) + costOfProduct;
  };

  print(totalCostOfProduct(taxRate: taxRate, costOfProduct: costOfProduct));

  String tellMeThePrice({required Function totalCostOfProduct}) {
    return "THE PRICE IS ${totalCostOfProduct}";
  }

  print(
    tellMeThePrice(
      totalCostOfProduct: totalCostOfProduct(
        taxRate: taxRate,
        totalCostOfProduct: costOfProduct,
      ),
    ),
  );
}

Running this in DartPad gives the following error:

TypeError: totalCostOfProduct.call$2$taxRate$totalCostOfProduct is not a function
Error: TypeError: totalCostOfProduct.call$2$taxRate$totalCostOfProduct is not a function

There are a few things going on here I think:

  • Printing totalCostOfProduct in tellMeThePrice tries to print the function itself without printing the value returned by the function.
  • When tellMeThePrice is actually called, the code passes in the result of the function (a double) rather than the function itself. (Hence, the type error.)

In an attempt to solve these problems I tried the following modifications:

  String tellMeThePrice({required Function totalCostOfProduct}) {
    final result = totalCostOfProduct.call();
    return "THE PRICE IS $result";
  }

  print(
    tellMeThePrice(
      totalCostOfProduct: (taxRate, costOfProduct) => totalCostOfProduct(
        taxRate: taxRate,
        totalCostOfProduct: costOfProduct,
      ),
    ),
  );

This still returns an error:

TypeError: totalCostOfProduct.call$0 is not a function
Error: TypeError: totalCostOfProduct.call$0 is not a function

This is a contrived example by the reader to use two functions. However, it also stumped me. How do I pass the parameters on to the second function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2025-01-31 07:15:27

问题在于,Total Costoffroduct正在返回双数据类型,并且您将TotalCostofproduct作为Tellmetheprice函数中的函数。
因此,它将返回双数据类型而不是函数。

只需更改字符串tellmetheprice({必需函数totalCostofproduct}) 字符串tellmetheprice({必需double toul double costostofproduct})>
因为它返回双数据类型。

void main() {
  double taxRate = .0825;
  double costOfProduct = 9.99;

  Function totalCostOfProduct =
      ({required double taxRate, required double costOfProduct}) {
    return (costOfProduct * taxRate) + costOfProduct;
  };

  print(totalCostOfProduct(taxRate: taxRate, costOfProduct: costOfProduct));

  String tellMeThePrice({required double totalCostOfProduct}) {
    return "THE PRICE IS ${totalCostOfProduct}";
  }

  print(
    tellMeThePrice(
      totalCostOfProduct: totalCostOfProduct(
        taxRate: taxRate,
        costOfProduct: costOfProduct,
      ),
    ).toString()
  );     // THE PRICE IS 10.814175
}

The problem is that totalCostOfProduct is returning double data type and you are taking totalCostOfProduct as a function in the tellMeThePrice function.
So it will return a double data type instead of a function.

just change String tellMeThePrice({required Function totalCostOfProduct}) to String tellMeThePrice({required double totalCostOfProduct})
because it'e returning double data type.

void main() {
  double taxRate = .0825;
  double costOfProduct = 9.99;

  Function totalCostOfProduct =
      ({required double taxRate, required double costOfProduct}) {
    return (costOfProduct * taxRate) + costOfProduct;
  };

  print(totalCostOfProduct(taxRate: taxRate, costOfProduct: costOfProduct));

  String tellMeThePrice({required double totalCostOfProduct}) {
    return "THE PRICE IS ${totalCostOfProduct}";
  }

  print(
    tellMeThePrice(
      totalCostOfProduct: totalCostOfProduct(
        taxRate: taxRate,
        costOfProduct: costOfProduct,
      ),
    ).toString()
  );     // THE PRICE IS 10.814175
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文