flutter - 飞镖类中的错误
问题描述
所以我正在开发这个测验应用程序,我只是为我的问题和答案创建一个类。但是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.
解决方案
首先你可以省略这部分。
domande = d;
risposte = r;
将构造函数参数分配给实例变量的模式非常常见,但是使用 dart 你可以像下面的代码片段那样做。
现在我们来解决您的问题。首先,您使用的是null-safety并将您的变量创建为 not 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});
}
推荐阅读
- node.js - 是否可以从 Express 服务器设置 Firebase Analytics?
- json - JSLT:输出非 JSON
- git - 在另一个分支中检测提交差异/补丁/大块,类似于cherry-pick的方式
- php - 将 php 7.1 与 Laravel Valet 2.7.0 一起使用时出现错误
- mysql - 如何在 MySQL 中作为 OR 运算符参数的 Where 子句中使用子查询?
- java - 使用 Spring Cloud Gateway、Spring Microservices 和 Consul 实现 SSL/TLS
- sql - 如何在 SQLite 中使用 JSON 合并多个列?
- kubernetes - GKE 集群无法从 Gitlab 私有注册表中提取 (ErrImagePull)
- .net - 将用户控件的子控件绑定到 UC 的公共属性的问题
- elasticsearch - 从 Google Apps 脚本查询 ElasticSearch?