mirror of
https://github.com/HendrikRauh/inventree-app.git
synced 2026-01-13 19:46:25 +00:00
Adds helper methods for generic "model" class
- Will allow us to do some good refactoring
This commit is contained in:
parent
88a02eb9ef
commit
8cc767d019
1 changed files with 51 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import "dart:async";
|
||||
import "dart:convert";
|
||||
import "dart:io";
|
||||
|
||||
import "package:font_awesome_flutter/font_awesome_flutter.dart";
|
||||
|
|
@ -63,6 +64,56 @@ class InvenTreeModel {
|
|||
// Note: If the WEB_URL is the same (except for /api/) as URL then just leave blank
|
||||
String get WEB_URL => "";
|
||||
|
||||
// Helper function to set a value in the JSON data
|
||||
void setValue(String key, dynamic value) {
|
||||
jsondata[key] = value;
|
||||
}
|
||||
|
||||
// Helper function to get string value from JSON data
|
||||
String getString(String key, {String backup = ""}) {
|
||||
String value = backup;
|
||||
|
||||
if (jsondata.containsKey(key)) {
|
||||
value = jsondata[key].toString();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// Helper function to get integer value from JSON data
|
||||
int getInt(String key, {int backup = -1}) {
|
||||
int value = backup;
|
||||
|
||||
if (jsondata.containsKey(key)) {
|
||||
value = int.tryParse(jsondata[key].toString()) ?? backup;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// Helper function to get double value from JSON data
|
||||
double getDouble(String key, {double backup = 0.0}) {
|
||||
double value = backup;
|
||||
|
||||
if (jsondata.containsKey(key)) {
|
||||
value = double.tryParse(jsondata[key].toString()) ?? backup;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// Helper function to get boolean value from json data
|
||||
bool getBool(String key, {bool backup = false}) {
|
||||
bool value = backup;
|
||||
|
||||
if (jsondata.containsKey(key)) {
|
||||
String str_value = (jsondata[key] ?? backup.toString()) as String;
|
||||
value = str_value.toLowerCase() == "true";
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
String get webUrl {
|
||||
|
||||
if (api.isConnected()) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue