所以我正在开发这个测验应用程序,我只是为我的问题和答案创建了一个类。但是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.发布于 2021-07-31 08:43:51
首先,你可以省略这部分。
domande = d;
risposte = r;将constructor参数赋值给实例变量的模式非常常见,但是使用dart,您可以像下面的代码片段一样执行此操作。
现在我们来解决你的问题。首先,您使用的是null-safety,并创建了不为nullable的变量。然后你用optional parameters创建了你的构造器,这意味着那些参数可以是null,但是你的类变量不能是空的。这就是发生此错误的原因。
你可以通过使用关键字required来解决这个问题,这意味着你的变量是mandatory。
class Domande {
String domande;
bool risposte;
Domande({required this.domande, required this.risposte});
}解决这个问题的另一种方法是使用?将变量设置为nullable。但是,如果这些变量为null,请确保您处理了这种情况。
class Domande {
String? domande;
bool? risposte;
Domande({this.domande, this.risposte});
}发布于 2021-07-31 08:45:19
错误与Null Safety in Flutter有关。这基本上意味着,您需要处理代码上的空值。
当您使用以下代码时:
class Domande {
String domande;
bool risposte;
Domande({String d, bool r}) {
domande = d;
risposte = r;
}
}在构造函数中,Domande({String d, bool r})您定义了一个非必需的参数,因此您可以使用以下代码:
var item = Domande();这使得参数d和r缺省为空。但空安全功能会强制您定义可为空的参数。因此,您需要将代码更改为:
class Domande {
String? domande; // nullable
bool? risposte; // nullable
Domande({String d, bool r}) {
domande = d;
risposte = r;
}
}然后,无论何时使用Domande类,都需要处理domande和risposte的空值。
或者,您可以通过向参数添加required关键字来强制Domande创建始终具有非空值:
class Domande {
String domande;
bool risposte;
Domande({required String d, required bool r}) {
domande = d;
risposte = r;
}
}因此,您不需要处理空值。但是您必须使用以下代码创建对象:
var item = Domande(d: "d", r:false);https://stackoverflow.com/questions/68600441
复制相似问题