我的问题是,我现在可以将随机数据保存在单独的类的列表中,并通过单击flatbutton在同一屏幕上显示它,但是当我移至第二个屏幕时,我想从列表中查看相同的数据我收到一个错误,因为列表为空!
这是将数据发送到列表并在平面按钮上显示的页面
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,
),
),
)
}
答案 0 :(得分:0)
欢迎使用Stack Overflow,谢谢您的提问!您可以使用provider包在应用程序的不同页面中处理WordsSaved
类的状态。我在下面添加了一个代码示例,向您显示了如何执行此操作。如果您尚未阅读文档here,则可能需要更多有关如何处理Flutter应用程序中状态的信息。
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) {
return WordsSaved();
},
),
],
child: MyApp(),
));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: WordsCard(),
);
}
}
class WordsSaved extends ChangeNotifier {
final _wordsSaved = <String>[''];
List<String> get wordsSaved => List.unmodifiable(_wordsSaved);
void addWord(String word) {
_wordsSaved.add(word);
notifyListeners();
}
void removeWord(String word) {
_wordsSaved.remove(word);
notifyListeners();
}
}
class WordsCard extends StatefulWidget {
@override
_WordsCardState createState() => _WordsCardState();
}
class _WordsCardState extends State<WordsCard> {
final arabicWords = <String>[
'Aamer',
'Al-Abadi',
'Abargil',
'Abaza',
'Abbar',
];
int i = 0;
int _random;
int numberOfWords = 5;
@override
Widget build(BuildContext context) {
_random = Random().nextInt(arabicWords.length);
final wordsSaved = context.watch<WordsSaved>();
return Scaffold(
backgroundColor: Colors.white,
body: FlatButton(
padding: EdgeInsets.all(35),
onPressed: () {
if (wordsSaved.wordsSaved.length < numberOfWords) {
wordsSaved.addWord(arabicWords[_random]);
setState(() {
i++;
});
} else {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => TestMemory(),
),
);
}
},
child: Text(
wordsSaved.wordsSaved[i],
style: TextStyle(color: Colors.white, fontSize: 50),
textAlign: TextAlign.center,
maxLines: 2,
),
),
);
}
}
class TestMemory extends StatelessWidget {
final i = 0;
@override
Widget build(BuildContext context) {
final wordsSaved = context.watch<WordsSaved>();
return Scaffold(
backgroundColor: Colors.white,
body: FlatButton(
padding: EdgeInsets.all(35),
onPressed: () {},
child: Text(
wordsSaved.wordsSaved[i],
style: TextStyle(color: Colors.white, fontSize: 50),
textAlign: TextAlign.center,
maxLines: 2,
),
),
);
}
}