未传递参数值的颤振错误,(NoSuchMethodError:在 null 上调用了方法'[]'。接收器:空 尝试调用:[](0))



我在flutter中有一个项目,在那里我有getjson.json文件,我想列出它的列表,并将其作为参数传递给类,但在该类中,我无法在我的测试类中接收到作为列表的数据。我想将第一个问题显示为mydata[0][I.toString((],但它给出了错误方法"[]"是在null上调用的。接收方:null尝试调用:(这里是getjson代码:

class getJson extends StatelessWidget {
const getJson({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: DefaultAssetBundle.of(context).loadString("assets/quiz.json"),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text("there is an error");
}
// if (snapshot == null) {
//   return Scaffold(
//     body: const Center(
//       child: Text("List is empty.."),
//     ),
//   );
// } else
List? myData = json.decode(snapshot.data.toString());
// ignore: unnecessary_null_comparison
if (myData == null) {
// ignore: prefer_const_constructors
return Scaffold(
body: const Center(
child: Text("Loading.."),
),
);
} else {
// return Text("Data is $myData[0]");
return quiz(myData: myData, marks: 0, vie: 0, level: 0, uid: "0");
}
},
);
}
}

这是测验代码:

class quiz extends StatefulWidget {
final String uid;
// ignore: prefer_typing_uninitialized_variables
var mydata;
int marks;
int vie;
int level;
quiz(
{Key? key,
this.mydata,
required this.marks,
required this.vie,
required this.level,
required this.uid,
required List myData})
: super(key: key);
@override
// ignore: no_logic_in_create_state
State<quiz> createState() => _quizState(mydata, marks, vie, level, uid);
}
and my quiz class is 

class _quizState extends State<quiz> {
String message = "score" + " 0 " + "-" + "Best" + " 0 ";
String levels = "Level: " + "0" + "/21";
String vies = "5";
final String uid;
// ignore: prefer_typing_uninitialized_variables
var mydata;
int marks = 0;
int vie = 5;
int level = 0;
_quizState(this.mydata, this.marks, this.vie, this.level, this.uid);
Color ch = Colors.indigo;
Color rig = Colors.green;
Color wro = Colors.red;
int i = 1;
int timer = 30;
int click = 20;
String showTimer = "30";
Map<String, Color> buttoncolor = {
'a': Colors.indigoAccent,
'b': Colors.indigoAccent,
'c': Colors.indigoAccent,
'd': Colors.indigoAccent,
};
bool cancelTimer = false;
int g = 0;
void startTimer() async {
const onesec = Duration(seconds: 1);
Timer.periodic(onesec, (Timer t) {
setState(() {
if (timer < 1) {
t.cancel();
nextQuestion();
} else if (cancelTimer == true) {
t.cancel();
} else {
timer = timer - 1;
}
showTimer = timer.toString();
});
});
}
void nextQuestion() {
click = 20;
cancelTimer = false;
timer = 30;
setState(() {
if (i < 20) {
level++;
levels = 'level: ' + "$level" + "/20";
i++;
} else {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => result(marks: marks)));
}
buttoncolor["a"] = Colors.indigoAccent;
buttoncolor["b"] = Colors.indigoAccent;
buttoncolor["c"] = Colors.indigoAccent;
buttoncolor["d"] = Colors.indigoAccent;
});
startTimer();
}
void quit() {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => result(marks: marks)));
}
void checkAnswer(String k) async {
if (click == 20) {
if (mydata[2][i.toString()] == mydata[1][i.toString()][k]) {
marks = marks + 5;
message = "Score" + "$marks" + "-" + "Best" + g.toString();
ch = rig;
} else {
ch = wro;
vie = vie - 1;
vies = "$vie";
}
setState(() {
buttoncolor[k] = ch;
if (k != "a" &&
mydata[2][i.toString()] == mydata[1][i.toString()]['a']) {
buttoncolor["a"] = rig;
}
if (k != "b" &&
mydata[2][i.toString()] == mydata[1][i.toString()]['b']) {
buttoncolor["b"] = rig;
}
if (k != "c" &&
mydata[2][i.toString()] == mydata[1][i.toString()]['c']) {
buttoncolor["c"] = rig;
}
if (k != "d" &&
mydata[2][i.toString()] == mydata[1][i.toString()]['c']) {
buttoncolor["c"] = rig;
}
cancelTimer = true;
});
click = 30;
if (vie > 0) {
Timer(const Duration(seconds: 2), nextQuestion);
} else {
Timer(const Duration(seconds: 2), quit);
}
}
}
Widget choiicebutton(String k) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 0.0),
child: MaterialButton(
onPressed: () => {checkAnswer(k)},
child: Text(
mydata[1][i.toString()][k],
style: const TextStyle(
fontSize: 14.0,
color: Colors.white,
fontWeight: FontWeight.bold,
),
maxLines: 1,
),
color: buttoncolor[k],
splashColor: Colors.indigo[700],
highlightColor: Colors.indigo[700],
minWidth: 400.0,
height: 45.0,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => accueil()));
},
),
title: Text("ART QUICK QUIZ"),
actions: [
Text(
vies,
// ignore: prefer_const_constructors
style: TextStyle(
height: 2, fontWeight: FontWeight.bold, fontSize: 20.0),
),
IconButton(
icon: Icon(
Icons.favorite,
color: Colors.deepOrange,
),
onPressed: () {},
)
],
toolbarHeight: 50.0,
centerTitle: true,
),
body: Scrollbar(
child: Container(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(1),
color: Colors.indigoAccent,
gradient: LinearGradient(
colors: [Colors.indigo.shade100, Colors.indigo.shade200],
begin: Alignment.bottomLeft,
end: Alignment.bottomRight,
),
),
child: Column(
children: [
Expanded(
flex: 1,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
margin: EdgeInsets.all(10.0),
width: 180,
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(10.0),
color: Colors.indigoAccent,
),
child: Text(
message,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 18.0,
),
),
),
Container(
margin: EdgeInsets.all(10.0),
width: 160,
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(10.0),
color: Colors.indigoAccent,
),
child: Text(
levels,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 18.0,
),
),
),
],
),
),
Expanded(
flex: 1,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(15.0),
color: Colors.indigoAccent,
),
padding: EdgeInsets.fromLTRB(5, 1, 5, 0),
margin: EdgeInsets.fromLTRB(5, 1, 5, 0),
alignment: Alignment.center,
child: Text(
mydata[0][i.toString()],
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
),
),
),
),
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.fromLTRB(5, 1, 5, 0),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
choiicebutton("a"),
choiicebutton("b"),
choiicebutton("c"),
choiicebutton("d"),
],
),
),
),
Expanded(
flex: 1,
child: Container(
margin: EdgeInsets.all(0.0),
height: 120,
width: 120.0,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(20.0),
color: Colors.indigoAccent,
boxShadow: kElevationToShadow[8],
),
child: Center(
child: Text(
showTimer,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 40.0,
color: Colors.white,
fontFamily: "Times New Roman",
),
)),
),
),
],
),
)),
);
}
}

我得到了解决方案,问题是传递给测验的参数名称,它应该与测验中的decleared相同。

最新更新