【发布时间】:2021-07-31 08:26:32
【问题描述】:
所以我正在开发这个测验应用程序,我只是为我的问题和答案创建一个类。但是vscode一直告诉我有错误。有人可以帮帮我吗?
这里是 main.dart
import 'package:flutter/material.dart';
import 'question.dart';
void main() {
runApp(HomePage());
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.grey[900],
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: Quizzler(),
),
),
),
);
}
}
class Quizzler extends StatefulWidget {
@override
QuizzlerState createState() => QuizzlerState();
}
class QuizzlerState extends State<Quizzler> {
List<Widget> scoreKeeper = [];
List<Domande> domandeBank = [
Domande(d: 'Il sole è una stella', r: true),
Domande(d: 'Il latte è verde', r: false),
Domande(d: 'Il mare è blu', r: true),
];
int qNumber = 0;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Center(
child: Text(
domandeBank[qNumber].domande,
style: TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: TextButton(
style: TextButton.styleFrom(padding: EdgeInsets.zero),
child: Container(
color: Colors.green,
height: 50,
width: double.infinity,
child: Center(
child: Text(
"True",
style: TextStyle(color: Colors.white),
),
),
),
onPressed: () {
setState(() {
bool risCorretta = domandeBank[qNumber].risposte;
if (risCorretta == true) {
scoreKeeper.add(
Icon(
Icons.check,
color: Colors.green,
),
);
} else {
scoreKeeper.add(
Icon(
Icons.close,
color: Colors.red,
),
);
}
qNumber++;
});
},
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: TextButton(
style: TextButton.styleFrom(padding: EdgeInsets.zero),
child: Container(
color: Colors.red,
height: 50,
width: double.infinity,
child: Center(
child: Text(
"False",
style: TextStyle(color: Colors.white),
),
),
),
onPressed: () {
setState(() {
bool risCorretta = domandeBank[qNumber].risposte;
if (risCorretta == false) {
scoreKeeper.add(
Icon(
Icons.check,
color: Colors.green,
),
);
} else {
scoreKeeper.add(
Icon(
Icons.close,
color: Colors.red,
),
);
}
qNumber++;
});
},
),
),
),
Row(
children: scoreKeeper,
),
],
);
}
}
这里是 question.dart 类
class Domande {
String domande;
bool risposte;
Domande({String d, bool r}) {
domande = d;
risposte = r;
}
}
我得到的错误:
Non-nullable instance field 'domande' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
Non-nullable instance field 'risposte' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
The parameter 'd' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
The parameter 'r' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
【问题讨论】:
标签: flutter dart dart-null-safety