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");
},
)
Solution:
String? qrcode = result?.code??"";
late Message data = Message.fromJson(jsonDecode((qrcode??"")));