Math 在dart中使用parseInt时服务器崩溃?

Math 在dart中使用parseInt时服务器崩溃?,math,numbers,dart,Math,Numbers,Dart,我不知道我做错了什么。我有一个很小的程序来解析本地主机上的请求。我的客户有这样一个问题: var local_num = null; // init to null // my server is running on localhost at the moment // using XMLHttpRequest I send it the value (it woud still be null) request.open('GET', 'http://127.0.0.1:8080?playe

我不知道我做错了什么。我有一个很小的程序来解析本地主机上的请求。我的客户有这样一个问题:

var local_num = null; // init to null
// my server is running on localhost at the moment
// using XMLHttpRequest I send it the value (it woud still be null)
request.open('GET', 'http://127.0.0.1:8080?player=${local_num}', false);
我的服务器代码应该能够处理以下问题:

action = params['action'];
var player_num = params['player'];
print ("got player num ${player_num}");
print(player_num is num); // false in both cases (see log below)
print(player_num is int);

if (player_num == null) { // never reaches inside of this if clause
print("received num is null, skipping parsing");
} else {
  try {
    var actual_number = Math.parseInt(received_num);
    print("parsed"); // never gets here
  } catch(var e) {
print(e); // should throw...
  }
}
服务器记录以下内容:

// it gets the request, player is null...
Instance of '_HttpRequest@14117cc4'
{player: null}
got player num null
false
false
但随后,砰的一声!->在dart:bootstrap\u impl。。。绕1255线

int pow(int exponent) {
  throw "Bigint.pow not implemented";
}

我目前看到的主要问题是:

if (player_num == null) {
在本例中,您将检查player_num是否为空值。但是,您不会检查该值是否为字符串“null”,在本例中,它似乎是空的

从现在起,当您将其传递给parseInt时,它应该抛出FormatException(注意:不应该使用Math.static类,因为它是dart:Math中的顶级函数。静态类Math.仍然来自dart:core)


也就是说,我的try/catch使用parseInt('null');确实捕捉到了错误。让我感到困惑的是,您的错误表明正在显示的错误是针对“pow()”函数的,与parseInt()完全无关。

目前我看到的主要问题是:

if (player_num == null) {
在本例中,您将检查player_num是否为空值。但是,您不会检查该值是否为字符串“null”,在本例中,它似乎是空的

从现在起,当您将其传递给parseInt时,它应该抛出FormatException(注意:不应该使用Math.static类,因为它是dart:Math中的顶级函数。静态类Math.仍然来自dart:core)


也就是说,我的try/catch使用parseInt('null');确实捕捉到了错误。让我感到困惑的是,您的错误表明显示的错误是针对“pow()”函数的,与parseInt()完全无关。

如果player是一个实际数字而不是一个空字符串(params['player']),它会失败什么。@LarsTackmann因此,当我显式地将URL参数设置为int时,它工作得很好(即将其设置为
1
或其他)。它仍然会触发解析逻辑,因此进入时,它必须认为它是一个字符串,然后将其转换为int。因此我感到困惑。我确实检查
==null
,但该语句从未到达。也许有另一种方法检查null…但除此之外-我不应该得到异常吗???它失败了什么如果player是一个实际的数字而不是一个空字符串(params['player'])。@LarsTackmann因此,当我将URL参数显式设置为int时,它工作得很好(即将其设置为
1
或其他)。它仍然会触发解析逻辑,因此进入时,它必须认为它是一个字符串,然后将其转换为int。因此我感到困惑。我确实检查了
==null
,但该语句从未到达。也许有另一种方法检查null…但除此之外-我不应该得到异常吗???抱歉-在为Stackoverflow重写较短版本的代码时,这是一个复制/粘贴错误。我添加了一个
#import('dart:math')
语句,但在
dart:bootstrap\u impl
第1256行附近
int-pow(int-exponent){throw“Bigint.pow未实现”}
它在类中
类Bigint扩展IntegerImplementation实现int
…而且我没有使用特定的函数来解析URL参数。因为它们是作为映射出现的,所以我只抓取键上的任何内容…是的,一定是这样。即不是“null”,而是字符串null。它将成为pow()真奇怪)…对此很抱歉-在为Stackoverflow重写较短版本的代码时,这是一个复制/粘贴错误。我添加了一个
#import('dart:math')
语句,但在
dart:bootstrap\u impl
第1256行附近
int-pow(int-exponent){throw“Bigint.pow未实现”;}
它在类中
类Bigint扩展IntegerImplementation实现int
…而且我没有使用特定的函数来解析URL参数。因为它们是作为映射出现的,所以我只抓取键上的任何内容…是的,一定是这样。例如,不是“null”,而是字符串null。它将成为pow(),这很奇怪。。。