在 Flutter 的 onChange 方法中,参数类型 'String' 不能分配给参数类型 'num'

The argument type 'String' can't be assigned to the parameter type 'num' in Flutter's onChange method

提问人:Ahmed Adel 提问时间:10/31/2023 更新时间:10/31/2023 访问量:52

问:

当我尝试在我的 Cubit 的 onChange 方法中记录 Change 对象时,我在我的 Flutter 应用程序中遇到了一个错误。错误消息如下:

Error: The argument type 'String' can't be assigned to the parameter type 'num'

此错误指向我使用 log(change.toString());记录 Change 对象的行。我知道发生此错误是因为我尝试将字符串记录为 num,这在 Dart 中是不允许的。

我想了解如何在不遇到此错误的情况下正确地将 Change 对象记录为“字符串”。

下面是相关的代码片段: '''

@override
void onChange(Change<PaymentState> change) {
  log(change.toString()); // Error occurs on this line
  super.onChange(change);
}

我将不胜感激有关如何解决此错误并将 Change 对象正确记录为 Flutter 中的“字符串”的任何见解或建议。

Flutter dart 条纹支付 tostring

评论

0赞 SmellyCat 10/31/2023
这回答了你的问题吗?错误:在 Flutter 的 onChange 方法中,无法将参数类型 'String' 分配给参数类型 'num'
0赞 Ivo 10/31/2023
显然,这个函数只接受作为参数。我不知道你到底想做什么,但也许你想要或代替.在任何情况下,错误都出在此函数或您使用它的方式上lognumprintdebugPrintloglog
0赞 Md. Yeasin Sheikh 10/31/2023
尝试确保而不是import: dart:developerdart:math

答:

0赞 Cabdirashiid 10/31/2023 #1

使用喜欢debugPrint()debugPrnt(change.toString())

将消息打印到控制台,您可以使用 “flutter” 工具的 “logs” 命令(“flutter logs”)。


Edit 是 package:flutter/src/foundation/print.dart 的一个函数。debugPrint(String?, {int? wrapWidth})

用于飞镖print(Object? object)

将对象的字符串表示形式输出到控制台。