Flutterのこちらのチュートリアルに沿って以下のコードを書いていて、ちょっと気になる箇所がありました。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//...
Widget textSection = new Container(
padding: const EdgeInsets.all(32.0),
child: new Text(
'''
Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese
Alps.Situated 1,578 meters above sea level, it is one of the larger
Alpine Lakes. A gondola ride from Kandersteg, followed by a half-hour
walk through pastures and pine forest, leads you to the lake, which
warms to 20 degrees Celsius in the summer. Activities enjoyed here
include rowing, and riding the summer toboggan run.
''',
softWrap: true,
),
);
//...
}
'''Lake Oeschinen...'''
という文字列リテラルをnew Text()
の引数に渡してあげていますが、このLake Oeschinen...
という文字列がなぜ **シングルクォート1つ(')**あるいはダブルクォート('')ではなく、**シングルクォート3つ(''')**で囲われているのかなぁと疑問に思いました。マークダウンでもないし。
他言語でも使うのかもしれませんが、勉強不足で初めて見る感じ。
(ざっと調べた感じ、少なくとも他にScalaという言語では使うのでしょうか)
Dartのlanguage tourのStringの項目を見てみると、どうやらこれは('''
も含めて)複数行にわたって文字列リテラルを指定するときに使うそうです。
Another way to create a multi-line string: use a triple quote with either single or double quotation marks:
↑でいうtriple quote
が'''
です。
コード例は以下。
var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";
Javaみたいに+
を使ったり、StringBuilder
を使わなくても良くて楽ですね。