英文:
How to resolve null check operator of a variable within a builder?
问题
以下是您提供的代码的翻译部分:
我正在编写一个QR码扫描器的函数语句。当它没有面对QR码时,它应该在else if语句中处于等待状态。
```dart
else if (result == null) {
return Column(
children: [ const Text("欢迎!"), Text("费用:${load["Fee"]}"), ],
);
}
这里的问题是语句late Message data = Message.fromJson(jsonDecode((result!.code!)));
如果没有FutureBuilder的话是正常工作的。但是加上它后,它会显示一个异常,指向一个奇怪的变量语句,该异常为“Null check operator used on a null value”。
如何解决这个问题?我想通过else if语句在等待状态下显示。
Barcode? result;
FutureBuilder<DocumentSnapshot>(
future: FirebaseFirestore.instance
.collection('Drivers')
.doc(FirebaseAuth.instance.currentUser?.email)
.get(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) { return Text("出现了一些问题"); }
if (snapshot.hasData && !snapshot.data!.exists) { return Text("文档不存在"); }
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> load = snapshot.data!.data() as Map<String, dynamic>;
late Message data = Message.fromJson(jsonDecode((result!.code!))); // 除非没有Builder,否则运行时会出现Null check Operator错误。
int total = data.price * data.passenger;
return Column(
children: [
Row(children: [Text(load["name"]),],),
// <--- 这里有Builder
Container(
height: 100,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
// FutureBuilder<DocumentSnapshot>(), 需要访问乘客列表。&&(load["Fee"] == data.price)
Container(child: LayoutBuilder(builder: (context, constraints) {
if ((result != null) && (load["Fee"] == data.price)) {
return FutureBuilder<DocumentSnapshot>(
future: FirebaseFirestore.instance.collection('Users').doc(data.email).get(),
builder:(context, snapshot) {
if ((snapshot.hasError)||(snapshot.hasData && !snapshot.data!.exists)) { return const Text("出了些问题,请重试"); }
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> moneycheck = snapshot.data!.data() as Map<String, dynamic>;
if (moneycheck['Balance'] >= total){
//addPayRowUser(data.email, data.totalprice);
return Column(
children: [
Text("付款成功! \n ${DateTime.now().toString()}"),
Text("价格: RM${data.price} 每人"),
Text("总计: RM${data.totalprice}"),
Text("人数: ${data.passenger}"),
],
);
}
else if (moneycheck['Balance'] < total){ return
Column( children: [ Text("余额不足"),
Text("余额: RM ${moneycheck['Balance']}"),Text("价格: RM${data.price} 每人"),
Text("总计: RM${data.totalprice}"),Text("人数: ${data.passenger}"),
],
);
}
}
return const Text("加载中...");
},
);
}
else if (result == null) {
return Column(
children: [ const Text("欢迎!"), Text("费用:${load["Fee"]}"), ],
);
}
else if ((result != null) && (load["Fee"] != data.price))
{
return Column(
children: [ const Text("无效的QR码!"), Text("${data.price}"), Text("${data.totalprice}"),],
);
}
else {
return const Column(
children: [
Text("发生未知错误"),
],
);
}
})
// Text('Payment Made! \nPickup: ${data.pickup} \nDropoff: ${data.dropoff} \n Price:${data.balance}');
)
],
),
),
],
);
}
return const Text("加载中");
},
)
希望这有助于您理解代码。如果您需要任何进一步的帮助,请随时告诉我。
英文:
I'm doing a function statement of QR Code Scanner. When it's not facing the QR Code, it should be in waiting state in else if statement.
else if (result == null) {
return Column(
children: [ const Text("Welcome!"), Text("Fee:${load["Fee"]}"), ],
);
}
The problem here is the statement late Message data = Message.fromJson(jsonDecode((result!.code!)));
works well if there is no FutureBuilder. But with that, it shows an Exception, Null check operator used on a null value, pointing at the statement of a variable strangely enough.
How can I resolve that? I want to display in waiting state by an else if statement.
Barcode? result;
FutureBuilder<DocumentSnapshot>(
future: FirebaseFirestore.instance
.collection('Drivers')
.doc(FirebaseAuth.instance.currentUser?.email)
.get(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) { return Text("Something went wrong"); }
if (snapshot.hasData && !snapshot.data!.exists) { return Text("Document does not exist"); }
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> load = snapshot.data!.data() as Map<String, dynamic>;
late Message data = Message.fromJson(jsonDecode((result!.code!))); //Null check Operator Runtime Error unless no Builder there.
int total = data.price * data.passenger;
return Column(
children: [
Row(children: [Text(load["name"]),],),
//<--- Builder Here
Container(
height: 100,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
//FutureBuilder<DocumentSnapshot>(), Needs to access Passenger List. &&(load["Fee"] == data.price)
Container(child: LayoutBuilder(builder: (context, constraints) {
if ((result != null) && (load["Fee"] == data.price)) {
return FutureBuilder<DocumentSnapshot>(
future: FirebaseFirestore.instance.collection('Users').doc(data.email).get(),
builder:(context, snapshot) {
if ((snapshot.hasError)||(snapshot.hasData && !snapshot.data!.exists)) { return const Text("Something went wrong, Try again"); }
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> moneycheck = snapshot.data!.data() as Map<String, dynamic>;
if (moneycheck['Balance'] >= total){
//addPayRowUser(data.email, data.totalprice);
return Column(
children: [
Text("Payment Made! \n ${DateTime.now().toString()}"),
Text("Price: RM${data.price} per Person"),
Text("Total: RM${data.totalprice}"),
Text("Persons: ${data.passenger}"),
],
);
}
else if (moneycheck['Balance'] < total){ return
Column( children: [ Text("Insufficient Balance"),
Text("Balance: RM ${moneycheck['Balance']}"),Text("Price: RM${data.price} per Person"),
Text("Total: RM${data.totalprice}"),Text("Persons: ${data.passenger}"),
],
);
}
}
return const Text("Loading...");
},
);
}
else if (result == null) {
return Column(
children: [ const Text("Welcome!"), Text("Fee:${load["Fee"]}"), ],
);
}
else if ((result != null) && (load["Fee"] != data.price))
{
return Column(
children: [ const Text("Invalid QR Code!"), Text("${data.price}"), Text("${data.totalprice}"),],
);
}
else {
return const Column(
children: [
Text("Unknown Error Occured"),
],
);
}
})
//Text('Payment Made! \nPickup: ${data.pickup} \nDropoff: ${data.dropoff} \n Price:${data.balance}'),
)
],
),
),
],
);
}
return const Text("Loading");
},
)
答案1
得分: 0
String? qrcode = result?.code??"";
late Message data = Message.fromJson(jsonDecode((qrcode??"")));
英文:
Solution:
String? qrcode = result?.code??"";
late Message data = Message.fromJson(jsonDecode((qrcode??"")));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论