LoginSignup
0
0

Dartでリストからnullを削除する方法(Swift, Kotlin比較つき)

Posted at

状況

例えば以下のような配列があって、nullを排除したい

final list = [1,null,3,null,5]

Dartにはビルトインの高階関数が用意されていないが、whereTypeを用いたnullの排除ができる

final nonNullList = list.whereType<T>().toList() // 今回は`T=int`

補足

SwiftはcompactMapが用意されている

let list = [1,nil,3,nil,5]
let nonNilList = list.compactMap{ $0 }

KotlinはmapNotNullが用意されている

val list = listOf(1,null,3,null,5)
val nonNilList = list.mapNotNull{ it }
0
0
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
0
0