Flutter: How to retrieve the data of variables from the QR code I have generated with JSON and JSON decode?
Referring to someone's post, I'm trying to scan the QR Code I generated, in which I need to retrieve the data of a number of variables. This QR Code includes {"pickup":"Medan Gopeng Ipoh, Perak","dropoff":"Sungai Nibong, Penang", "searchdate":"Jul 1,2023","duration":"5:00 PM - 6:40 PM","totalprice":34,"passenger":2}
.
But when I scan the QR code I've generated, not only the scanning screen did not change as a failure, but I got an exception in the debug console.
D/DecoderThread(24361): Found barcode in 50 ms [log] FormatException: Scheme not starting with alphabetic character (at character 1) {"pickup":"Medan Gopeng Ipoh, Perak","dropoff":"Sungai Nibong, Penang","sea... ^
I want the variables to properly show their values. How can I resolve that?
These are the codes I've been working on.
Json Encode to QR Code Generator
Map<String, dynamic> myData = {
'pickup': pickup,
'dropoff': dropoff,
'searchdate': searchdate,
'duration': duration,
'totalprice': totalprice,
'passenger': passenger, //Number of passengers
};
String encodedJson = jsonEncode(myData);
child: QrImageView(
data: encodedJson,
size: 250,
embeddedImageStyle: const QrEmbeddedImageStyle(
size: Size(100,100,),
),
),
A file that's decoding QR Code Scanner while scanning the QR Code.
Future<GetProfile?> getProfileData(var url) async {
try {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final item = json.decode(response.body);
print(response.body);
profile = GetProfile.fromJson(item);
} else {
print("An error occured");
}
} catch (e) {
log(e.toString());
}
return profile;
}
A Json file for QR Code Scanner for decryption
import 'dart:convert';
GetData getProfileFromJson(String str) =>
GetData.fromJson(json.decode(str));
String getProfileToJson(GetData data) => json.encode(data.toJson());
class GetData {
GetData({
this.status,
this.message,
});
String? status;
Message? message;
factory GetData.fromJson(Map<String, dynamic> json) => GetData(
status: json["status"],
message: Message.fromJson(json["message"]),
);
Map<String, dynamic> toJson() => {
"status": status,
"message": message?.toJson(),
};
}
class Message {
final String pickup;
final String dropoff;
final String searchdate;
final String duration;
final int totalprice;
final int passenger;
Message({
required this.pickup,
required this.dropoff,
required this.searchdate,
required this.duration,
required this.totalprice,
required this.passenger,
});
Message.fromJson(Map<String, dynamic> json):
pickup = json["pickup"],
dropoff = json["dropoff"],
searchdate = json["searchdate"],
duration = json["duration"],
totalprice = json["totalprice"],
passenger = json["passenger"];
//createdAt: DateTime.parse(json["createdAt"]),
//final Map<String, dynamic> data = new Map<String, dynamic>();
Map<String, dynamic> toJson() => {
//final Map<String, dynamic> data = new Map<String, dynamic>();
"pickup": pickup,
"dropoff": dropoff,
"searchdate": searchdate,
"duration": duration,
"totalprice": totalprice,
"passenger": passenger,
//return data;
};
}