最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - Is it possible to use destructure and spread in a list with dart like javascript in dart? - Stack Overflow

programmeradmin2浏览0评论

I have been playing around with flutter for a few days now, haven't gotten a hang of it. While trying out the dart and its methods it got me thinking to make the learning curve easier started relating javascript (as I am very familiar with it, personal choice, people might have or use different approach).

I am stuck with this one small doubt, is it possible to destructure and spread at the same time in dart? I understand if it's not possible, how would I achieve something like this with dart? I am trying to get the first item of the array in a variable and the rest of the array values in a string with joining them as a string. Do I have to loop through all of them? Can't be in a shorter or prettier way(perspective tho)?

// JavaScript
var someArray = ['1', '2' ,'3', '4'];
var [first, ...rest] = someArray;
var restAsString = rest.join(' ');
console.log(first); // 1
console.log(restAsString); // 2 3 4
//dart
main(List<String> arguments) {
  List<String> someList = ['1', '2' ,'3', '4'];
  String restString = '';
  String firstString = '';
  for (int i = 0; i < someList.length; i++) {
    if(i == 0 ) {
      firstString = someList[i];
    } else {
      restString = '$restString ${someList[i]}';
    }

  }
  print(firstString); // 1
  print(restString);  // 2 3 4
}

I have been playing around with flutter for a few days now, haven't gotten a hang of it. While trying out the dart and its methods it got me thinking to make the learning curve easier started relating javascript (as I am very familiar with it, personal choice, people might have or use different approach).

I am stuck with this one small doubt, is it possible to destructure and spread at the same time in dart? I understand if it's not possible, how would I achieve something like this with dart? I am trying to get the first item of the array in a variable and the rest of the array values in a string with joining them as a string. Do I have to loop through all of them? Can't be in a shorter or prettier way(perspective tho)?

// JavaScript
var someArray = ['1', '2' ,'3', '4'];
var [first, ...rest] = someArray;
var restAsString = rest.join(' ');
console.log(first); // 1
console.log(restAsString); // 2 3 4
//dart
main(List<String> arguments) {
  List<String> someList = ['1', '2' ,'3', '4'];
  String restString = '';
  String firstString = '';
  for (int i = 0; i < someList.length; i++) {
    if(i == 0 ) {
      firstString = someList[i];
    } else {
      restString = '$restString ${someList[i]}';
    }

  }
  print(firstString); // 1
  print(restString);  // 2 3 4
}
Share Improve this question edited May 9, 2020 at 12:56 Subhendu Kundu asked May 9, 2020 at 12:48 Subhendu KunduSubhendu Kundu 3,8566 gold badges29 silver badges60 bronze badges 2
  • I see there's a close request for details or clarity require. How clear it can be? Just trying to understand the best practices or a better way to write dart.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论