【发布时间】:2020-07-03 17:56:09
【问题描述】:
我的问题是我现在可以将随机数据保存在单独类中的列表中,并通过单击平面按钮将其显示在同一屏幕上,但是当我移至第二个屏幕时,我想查看列表中的相同数据我收到了一个错误,因为列表是空的!!
这是将数据发送到列表并在平面按钮上显示的页面
WordsSaved wordSaved = WordsSaved();
TestMemory testMemory = TestMemory();
class WordsCard extends StatefulWidget {
@override
_WordsCardState createState() => _WordsCardState();
}
class _WordsCardState extends State<WordsCard> {
int i=0;
int _random;
int get random => _random;
String word ;
int numberOfWords = 5;
@override
Widget build(BuildContext context) {
_random = Random().nextInt(arabicWords.length);
double currentOpacity = 1;
return
Scaffold(
backgroundColor: Colors.white,
body:
FlatButton(
padding: EdgeInsets.all(35),
onPressed: ()=> {
setState(() {
if(wordSaved.wordsSaved.length < numberOfWords)
{
wordSaved.wordsSaved.add(arabicWords[_random]);
i++;
}
else
{
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => TestMemory()
),
);
print('The Words are Finished');
}
},
),
},
child:
AutoSizeText(
wordSaved.wordsSaved[i],
style: TextStyle(color: Colors.white, fontSize: 50),
textAlign: TextAlign.center,
maxLines: 2,
),
),
),
}
这是单独的类
class WordsSaved {
WordsSaved({ this.word}) ;
final String word;
int _random;
int get random => _random;
List<String> wordsSaved = [];
}
这是从列表中查看相同数据的声音屏幕
WordsSaved wordSaved = WordsSaved();
class TestMemory extends StatefulWidget {
@override
_TestMemoryState createState() => _TestMemoryState();
}
class _TestMemoryState extends State<TestMemory> {
int i=0;
@override
Widget build(BuildContext context) {
return
Scaffold(
backgroundColor: Colors.white,
body:
FlatButton(
padding: EdgeInsets.all(35),
onPressed: ()=> {
setState(() {
},
),
},
child:
AutoSizeText(
wordSaved.wordsSaved[i],
style: TextStyle(color: Colors.white, fontSize: 50),
textAlign: TextAlign.center,
maxLines: 2,
),
),
)
}
【问题讨论】:
-
您可能想查看您的单词保存类的 Get_It 包(Singleton)。访问从第一个屏幕保存的单词以保存数据。访问从第二个屏幕保存的单词以检索数据。 locator() 是你的朋友。