1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DartでListの文字を連結してStringにする

Posted at

Listの文字を結合して文字にしたい

DartでTagの機能を作っていてそのときに、そのまま送信すると、List<String>になる。AIにプロンプトを送る機能を作るのを想定してます。

[dart, flutter, android, ios, java, kotlin]

この場合はどうするのか?
join functionを使うと良い。
https://api.flutter.dev/flutter/package-path_path/join.html

使用例)

void main(List<String> args) {
  List<String> tags = ['dart', 'flutter', 'android', 'ios'];

  tags.add('java');
  tags.add('kotlin');
  print(tags);
  print(tags.runtimeType);
  print(tags.join(', '));
  print(tags.join(', ').runtimeType);
}

実行結果)

[dart, flutter, android, ios, java, kotlin]
List<String>
dart, flutter, android, ios, java, kotlin
String

Exited.

実行した結果だと.joinで結合する前は、List<String>ですがStringに変換されております。これでtagのテキストを文字として送ることができるようになるでしょう。

最後に

Listの文字を結合してただの文字にしたいときは、.joinを使いましょう。
IOTのお仕事をしたときは、APIから渡されてくる文字情報の結合に使っていましたね。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?