是否有更好的方法来解析一个int在Dart [英] Is there a better way to parse an int in Dart

查看:203
本文介绍了是否有更好的方法来解析一个int在Dart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Dart中的int.parse的几个问题...

This is several questions about int.parse in Dart...

我知道在Dart中我们可以解析一个字符串作为一个int和捕获异常使用类似:

I know that in Dart we can parse a string as an int and catch exceptions using something like:

try {
  n = int.parse(input.value);
  // etc.
} on FormatException {
  // etc.
}

(这是罚款。)

在文档中,有以下对int.parse的描述:

In the documentation, there is the following description of int.parse:

int parse(String source, int radix, int onError(String source))

然而,当我尝试对多个参数使用int.parse时,我得到了编辑关于我使用额外参数的投诉。我误解了文档吗?例如,如何设置基数?

When I tried using int.parse with more than one argument, however, I got a complaint from the editor about me using extra arguments. Am I misunderstanding the documentation? And how does one, for example, set the radix?

推荐答案

Int.parse 使用命名的可选参数。

Int.parse uses named, optional parameters.

API:

int parse(String source, {int radix, int onError(String source)})

{} 参数列表中的params周围的参数表示这些参数是可选的,名为的参数。
(如果您在参数列表中的参数周围有 [] 这些参数是可选,位置参数)

The { } around params in the parameter list indicate that these are optional, named parameters. (If you had [ ] around params in the parameter list, these would be optional, positional parameters)

示例用法:

int.parse("123");
int.parse("123", radix:16);
int.parse("123", onError:(source) => print("Source"));
int.parse("123", radix:16, onError:(source) => print(source));

这篇关于是否有更好的方法来解析一个int在Dart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆