diff --git a/lib/barcode.dart b/lib/barcode.dart index 07500d98..d145a58d 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -30,8 +30,7 @@ Future scanQrCode(BuildContext context) async { */ InvenTreeAPI().post("barcode/", body: {"barcode": barcode}).then((var response) { - // Close the progress dialog - Navigator.pop(context); + hideProgressDialog(context); if (response.statusCode != 200) { showDialog( diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 47b4bf6d..5f541361 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:InvenTree/api.dart'; import 'package:InvenTree/widget/dialogs.dart'; import 'package:flutter/cupertino.dart'; @@ -128,7 +130,23 @@ class InvenTreeModel { showProgressDialog(context, "Requesting Data", "Requesting ${NAME} data from server"); - var response = await api.get(addr, params: params); + var response = await api.get(addr, params: params) + .timeout(Duration(seconds: 10)) + .catchError((e) { + + hideProgressDialog(context); + + if (e is TimeoutException) { + showErrorDialog(context, "Timeout", "No response from server"); + } else { + showErrorDialog(context, "Error", e.toString()); + } + return null; + }); + + if (response == null) { + return null; + } hideProgressDialog(context); @@ -143,7 +161,7 @@ class InvenTreeModel { } // Return list of objects from the database, with optional filters - Future> list({Map filters}) async { + Future> list(BuildContext context, {Map filters}) async { if (filters == null) { filters = {}; @@ -162,7 +180,26 @@ class InvenTreeModel { // TODO - Add "timeout" // TODO - Add error catching - var response = await api.get(URL, params:params); + showProgressDialog(context, "Requesting Data", "Requesting ${NAME} data from server"); + + var response = await api.get(URL, params:params) + .timeout(Duration(seconds: 10)) + .catchError((e) { + + hideProgressDialog(context); + + if (e is TimeoutException) { + showErrorDialog(context, "Timeout", "No response from server"); + } else { + showErrorDialog(context, "Error", e.toString()); + } + + return null; + }); + + if (response == null) { + return null; + } // A list of "InvenTreeModel" items List results = new List();