如何在Flutter循环内创建一个异常

huangapple go评论62阅读模式
英文:

how to create an exception inside a flutter for loop

问题

我需要for循环中的项目执行某个操作,除了最后一个项目,它将执行不同的操作。

一个不起作用的示例:

for (var i = 0; i < 10; i++) {
  print('1 - 9');

  if (i === 9) {
    print('last item');
  }
}
英文:

I need the items inside a for to have an action with the exception of the last one, which will have a different action

an example that DOESN'T WORK:

for (var i = 0; i &lt; 10; i++) {
  print(&#39;1 - 9&#39;);


  if (i.last) {
    print(&#39;last item&#39;);
  }
}

答案1

得分: 1

尝试这个:

for (var i = 0; i < 10; i++) {
  print('0 - 8');

  if (i == 9) {
    print('last item');
  }
}
英文:

Try this:

for (var i = 0; i &lt; 10; i++) {
  print(&#39;0 - 8&#39;);

  if (i == 9) {
    print(&#39;last item&#39;);
  }
}

答案2

得分: 1

你可以尝试这样做

for (var i = 0; i < 10; i++) {
    print('1 - 9');

    if (i == 9) {
        print('last item');
        throw Exception("Your message");
    }
}
英文:

you can try this

for (var i = 0; i &lt; 10; i++) {
 print(&#39;1 - 9&#39;);


 if (i==9) {
    print(&#39;last item&#39;);
    throw Exception(&quot;Your message&quot;);
 }
 }

答案3

得分: 1

这也是可能的,你可以将这段代码粘贴到DartPad中,并进行测试。

void main() {
  try {
    for (var i = 0; i &lt; 10; i++) {
      if (i == 3) {
        print('item $i');
        throw 'customError';
      } else {
        print('item $i');
      }
    }
  } catch (e) {
    if (e == 'customError') {
      print('你捕获了自定义错误 \n $e');
    } else {
      print('你捕获了其他错误');
    }
  }
}

希望这对你有所帮助。

英文:

This is also possible, you can just paste the snippet in DartPad and play with it

void main() {
  try {
    for (var i = 0; i &lt; 10; i++) {
      if (i == 3) {
        print(&#39;item $i&#39;);
        throw &#39;customError&#39;;
      } else {
        print(&#39;item $i&#39;);
      }
    }
  } catch (e) {
    if (e == &#39;customError&#39;) {
      print(&#39;You caught your custom error \n $e&#39;);
    } else {
      print(&#39;You caught some other error&#39;);
    }
  }
}

Hope that will help

答案4

得分: 1

你可以将方法放在for循环的条件部分。以下是一个示例:

void main() {
  const limit = 10;
  for(int i = 0;
      () {
        if(i < limit && i < limit-1) {
          doActionA(i);
          return true;
        }
        else if(i < limit){
          doActionB(i);
          return true;
        }
        return false;
      }()
      
      ; i++);
}

void doActionA(int i){
  print('Not last $i');
}

void doActionB(int i) {
  print('last $i');
}

输出

Not last 0
Not last 1
Not last 2
Not last 3
Not last 4
Not last 5
Not last 6
Not last 7
Not last 8
last 9
英文:

You can put method in the condition part of the for loop. Here is such example:

void main() {
  const limit = 10;
  for(int i =0 ;
      () {
        if(i &lt; limit &amp;&amp; i &lt; limit-1) {
          doActionA(i);
          return true;
        }
        else if(i &lt; limit){
          doActionB(i);
          return true;
        }
        return false;
      }()
      
      ; i++);
}

void doActionA(int i){
  print(&#39;Not last $i&#39;);
}

void doActionB(int i) {
  print(&#39;last $i&#39;);
}

output

Not last 0
Not last 1
Not last 2
Not last 3
Not last 4
Not last 5
Not last 6
Not last 7
Not last 8
last 9

huangapple
  • 本文由 发表于 2023年2月9日 01:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定