From d0f1a4d4ced55827c301470c5be1145f08cbf0e5 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 14 Apr 2020 23:11:30 +1000 Subject: [PATCH] error dialog if the barcode cannot be converted to JSON! --- lib/barcode.dart | 10 +++++++++- lib/widget/dialogs.dart | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/barcode.dart b/lib/barcode.dart index 243e3497..8bc8bda3 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -21,8 +21,16 @@ Future scanQrCode(BuildContext context) async { print("Scanned: $result"); + Map data; + // Look for JSON data in the result... - final data = json.decode(result); + try { + data = json.decode(result); + } on FormatException { + showErrorDialog(context, "Bad barcode data", result); + + return; + } showProgressDialog(context, "Querying Server", "Sending barcode data to server"); diff --git a/lib/widget/dialogs.dart b/lib/widget/dialogs.dart index 5daa7092..db6a0eae 100644 --- a/lib/widget/dialogs.dart +++ b/lib/widget/dialogs.dart @@ -1,6 +1,25 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:font_awesome_flutter/font_awesome_flutter.dart'; + +void showErrorDialog(BuildContext context, String title, String description) { + showDialog( + context: context, + child: SimpleDialog( + title: ListTile( + title: Text("Error"), + leading: FaIcon(FontAwesomeIcons.exclamationCircle), + ), + children: [ + ListTile( + title: Text(title), + subtitle: Text(description) + ) + ] + ) + ); +} void showProgressDialog(BuildContext context, String title, String description) { showDialog(