我想呼叫這個方法但不能。我的目的是從文本變數中獲取字串,使用字串中的單詞顯示串列制作串列。我希望能做到這一點。有沒有其他方法或者我可以這樣做?
class SecondRoute extends StatefulWidget {
String text;
//const SecondRoute({Key? key}) : super(key: key);
SecondRoute({Key? key, required this.text}) : super(key: key);
@override
_SecondRouteState createState() => _SecondRouteState();
}
class _SecondRouteState extends State<SecondRoute> {
String newT ="";
List breakText(){
newT = widget.text;
var x = newT.split(" ");
print(x);
bool res = x.remove("");
while(res == true){
res = x.remove("");
}
print(x);
return x;
}
List wordlist = breakText();// can not call this method
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Second Route"),
),
body: Center(
child: Text(
"hhhh",
// text,
style: TextStyle(fontSize: 24),
),
),
);
}
}
uj5u.com熱心網友回復:
您需要在呼叫時初始化您的資料 _SecondRouteState
class SecondRoute extends StatefulWidget {
String text;
//const SecondRoute({Key? key}) : super(key: key);
SecondRoute({Key? key, required this.text}) : super(key: key);
@override
_SecondRouteState createState() => _SecondRouteState();
}
class _SecondRouteState extends State<SecondRoute> {
String newT ="";
List breakText(){
newT = widget.text;
var x = newT.split(" ");
print(x);
bool res = x.remove("");
while(res == true){
res = x.remove("");
}
print(x);
return x;
}
List wordlist; // declare your list
@override
void initState() {
super.initState();
wordlist = breakText(); // initialize from here
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Second Route"),
),
body: Center(
child: Text(
"hhhh",
// text,
style: TextStyle(fontSize: 24),
),
),
);
}
}
uj5u.com熱心網友回復:
你應該移動“List wordlist = breakText();” 進入 initState() 或 build() 方法。
uj5u.com熱心網友回復:
1-定義串列:
List wordlist ;
2-初始化:
@override
void initState(){
wordlist = breakText();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/367003.html
上一篇:修正顫振側選單中的白色顯示
下一篇:Dart:遍歷串列中的串列
