Flutter 如何在 Flutter Application(Android Studio Emulator) 中显示字符串
Flutter how to display string in Flutter Application(Android Studio Emulator)
我在 Flutter App 中显示字符串输出时遇到问题,我想像这样在列中显示字符串值:
I
want
to
split
this
但我得到了这个输出:
[I, want, to, split, this]
我不知道该怎么做,因为我还是编程新手,但我认为下面这段代码一定是原因:
void _splitWordInColumn(){
setState(() {
sentenceToWord.forEach((e) => print(e));
});
}
这是错误输出的 image 下面是我的完整代码:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> sentenceToWord = 'I want to split this'.split(" ");
void _splitWordInColumn(){
setState(() {
sentenceToWord.forEach((e) => print(e));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo HomePage'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(sentenceToWord.toString()),
],
),
),
);
}
}
希望有人能帮我解决这个问题,谢谢。
试试这个:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String sentenceToWord = 'I \nwant \nto \nsplit \nthis';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo HomePage'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(sentenceToWord),
],
),
),
);
}
}
尝试将 map
运算符与 column
一起使用。
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: sentenceToWord.map((e) => Text(e)).toList(),
),
我在 Flutter App 中显示字符串输出时遇到问题,我想像这样在列中显示字符串值:
I
want
to
split
this
但我得到了这个输出:
[I, want, to, split, this]
我不知道该怎么做,因为我还是编程新手,但我认为下面这段代码一定是原因:
void _splitWordInColumn(){
setState(() {
sentenceToWord.forEach((e) => print(e));
});
}
这是错误输出的 image 下面是我的完整代码:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> sentenceToWord = 'I want to split this'.split(" ");
void _splitWordInColumn(){
setState(() {
sentenceToWord.forEach((e) => print(e));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo HomePage'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(sentenceToWord.toString()),
],
),
),
);
}
}
希望有人能帮我解决这个问题,谢谢。
试试这个:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String sentenceToWord = 'I \nwant \nto \nsplit \nthis';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo HomePage'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(sentenceToWord),
],
),
),
);
}
}
尝试将 map
运算符与 column
一起使用。
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: sentenceToWord.map((e) => Text(e)).toList(),
),