所以我有一个包含一堆项目的动画列表和一个用户可以切换的过滤器(开关),过滤器将删除不需要的项目,但由于某种原因我不断得到:
<块引用>"处理手势时抛出以下 NoSuchMethodError: 在 null 上调用了 getter 'currentState'。接收器:空尝试 调用:currentState",
但如果我一直按开关,代码确实有效。
这是我删除项目的方式:
void removeItems(){
if(onlyRight){
for(int i = 0; i < mainItems.length; i++){
QueList item = mainItems[i];
if(item.isRight==false){
mainItems.removeAt(i);
_listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
item: item,
animation: animation,
onlyRight: onlyRight,
onlyWrong: onlyWrong,
)
);
}
}
}
if(onlyWrong){
for(int i = 0; i < mainItems.length; i++){
QueList item = mainItems[i];
if(item.isRight!=false){
mainItems.removeAt(i);
_listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
item: item,
animation: animation,
onlyRight: onlyRight,
onlyWrong: onlyWrong,
)
);
}
}
}
if(onlyRight==false && onlyWrong == false){
Navigator.of(context).pushReplacementNamed('/view');
}
}
这是动画列表:
void removeItems(){
if(onlyRight){
for(int i = 0; i < mainItems.length; i++){
QueList item = mainItems[i];
if(item.isRight==false){
mainItems.removeAt(i);
_listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
item: item,
animation: animation,
onlyRight: onlyRight,
onlyWrong: onlyWrong,
)
);
}
}
}
if(onlyWrong){
for(int i = 0; i < mainItems.length; i++){
QueList item = mainItems[i];
if(item.isRight!=false){
mainItems.removeAt(i);
_listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
item: item,
animation: animation,
onlyRight: onlyRight,
onlyWrong: onlyWrong,
)
);
}
}
}
if(onlyRight==false && onlyWrong == false){
Navigator.of(context).pushReplacementNamed('/view');
}
}
最后这是整个脚本:
import 'package:flutter/material.dart';
import 'package:liquid_swipe/liquid_swipe.dart';
import 'package:qudorat_app/prosses/card_que_view.dart';
import 'package:qudorat_app/prosses/question_list.dart';
import 'package:qudorat_app/prosses/temp_list.dart';
class ViewAnswers extends StatefulWidget {
final GlobalKey<AnimatedListState> keyer;
const ViewAnswers({required this.keyer});
@override
_ViewAnswersState createState() => _ViewAnswersState();
}
class _ViewAnswersState extends State<ViewAnswers> {
bool onlyRight = false;
bool onlyWrong = false;
late GlobalKey<AnimatedListState> _listKey= widget.keyer;
List<QueList> mainItems = [];
List<QueList> items = List.from(queListItems);
@override
void initState() {
super.initState();
mainItems = items;
}
void removeItems(){
if(onlyRight){
for(int i = 0; i < mainItems.length; i++){
QueList item = mainItems[i];
if(item.isRight==false){
mainItems.removeAt(i);
_listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
item: item,
animation: animation,
onlyRight: onlyRight,
onlyWrong: onlyWrong,
)
);
}
}
}
if(onlyWrong){
for(int i = 0; i < mainItems.length; i++){
QueList item = mainItems[i];
if(item.isRight!=false){
mainItems.removeAt(i);
_listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
item: item,
animation: animation,
onlyRight: onlyRight,
onlyWrong: onlyWrong,
)
);
}
}
}
if(onlyRight==false && onlyWrong == false){
Navigator.of(context).pushReplacementNamed('/view');
}
}
@override
Widget build(BuildContext context) {
print(mainItems.length);
return LiquidSwipe(pages: [
Scaffold(
backgroundColor: Colors.amber,
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.pink, Colors.amber])),
child: Center(
child: SafeArea(
child: Column(children: [
Row(
children: [
Expanded(
child: Container(
margin: EdgeInsets.all(8),
padding: EdgeInsets.fromLTRB(8, 12, 8, 12),
decoration: BoxDecoration(
color: Colors.lightBlueAccent,
borderRadius: BorderRadius.circular(25.0),
boxShadow: [
BoxShadow(
color: Colors.black54,
blurRadius: 15.0,
offset: Offset(0.0, 0.75))
],
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Switch(
activeColor: Colors.grey[800],
value: onlyRight,
onChanged: onlyWrong ? null: (b) {
setState(() {
onlyRight = b;
removeItems();
});
},
),
Text(
"الإجابات \n الصحيحة",
textDirection: TextDirection.rtl,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontFamily: "ArabicModern-Bold"),
),
SizedBox(
width: 100,
),
// add math arabic
Switch(
activeColor: Colors.grey[800],
value: onlyWrong,
onChanged: onlyRight ? null: (b) {
setState(() {
onlyWrong = b;
removeItems();
});
},
),
Text(
"الإجابات \n الخاطئة",
textDirection: TextDirection.rtl,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontFamily: "ArabicModern-Bold"),
),
],
),
)),
],
),
Expanded(
child: Container(
padding: EdgeInsets.all(12.0),
child: AnimatedList(
initialItemCount: mainItems.length,
key: _listKey,
itemBuilder: (context, index, animation) {
return QueViewWidget(
item: mainItems[index],
animation: animation,
onlyRight: onlyRight,
onlyWrong: onlyWrong,
);
},
),
))
]),
),
),
),
),
]);
}
}
如果您对如何解决此问题有任何想法,请帮助我,因为我一直在尝试解决此问题
答案 0 :(得分:0)
_listKey
为 null,仔细检查传递给构造函数 ViewAnswers()
的值是否为非 null 或尝试将 var init _listKey= widget.keyer
移动到 initstate 方法
答案 1 :(得分:0)
解决方案 1 如果您只有一个 ViewAnswers Widget 实例
我建议您将 listKey 定义为静态变量,而不是在小部件之间传递它
class ViewAnswersState extends State<ViewAnswers> {
static GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();
...
并在像这样的顶级小部件中使用它
SignUpScreenState.listKey.method();
解决方案 2
按如下方式编辑您的 ViewAnswersState
class ViewAnswersState extends State<ViewAnswers> {
GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();
addItem(x){
// code to add item to be used parent class
}
doSometing(){//}
...
此后,在您想要创建此 ViewAnswers 时在您的父小部件中
GlobalKey<ViewAnswersState> viewAnswersKey = GlobalKey<ViewAnswersState>();
...
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>ViewAnswers(key:viewAnswersKey)));
然后你可以简单地使用 ViewAnswersState 中的函数
viewAnswersKey.currentState.addItem(x)