2019-07-16 15:55:00 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:local_spend/common/functions/get_token.dart';
|
2019-08-07 12:15:15 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2019-07-16 15:55:00 +00:00
|
|
|
|
2019-08-12 11:09:04 +00:00
|
|
|
Future<List<String>> getCategories() async {
|
2019-08-20 12:54:45 +00:00
|
|
|
const url = "https://dev.localspend.co.uk/api/search/category";
|
2019-07-16 15:55:00 +00:00
|
|
|
var token;
|
|
|
|
|
|
|
|
await getToken().then((result) {
|
|
|
|
token = result;
|
|
|
|
});
|
|
|
|
|
|
|
|
Map<String, String> body = {
|
|
|
|
"session_key":token,
|
|
|
|
};
|
|
|
|
|
|
|
|
final response = await http.post (
|
|
|
|
url,
|
|
|
|
body: json.encode(body),
|
|
|
|
);
|
|
|
|
|
|
|
|
// print(response.body);
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
//request successful
|
2019-08-12 11:09:04 +00:00
|
|
|
List<String> categories = new List<String>();
|
2019-07-16 15:55:00 +00:00
|
|
|
Map responseMap = json.decode(response.body);
|
|
|
|
|
|
|
|
var categoriesJSON = responseMap['categories'];
|
|
|
|
|
|
|
|
//nice.
|
|
|
|
// so the response needs to be decoded iteratively, like
|
|
|
|
// categories.add(new Category(name: categoriesJSON[i.toString()], index: i.toString()));
|
|
|
|
|
|
|
|
// print(categoriesJSON['11']); // prints "Banana"
|
|
|
|
|
|
|
|
int i = 1; // starts on 1. that was annoying to debug!
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
if (categoriesJSON[i.toString()] != null) {
|
|
|
|
// print("Iteration " + i.toString());
|
|
|
|
// print(categoriesJSON[i.toString()]);
|
2019-08-12 11:09:04 +00:00
|
|
|
categories.add(categoriesJSON[i.toString()]);
|
2019-07-16 15:55:00 +00:00
|
|
|
// print(categories.last);
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
// print("Number of categories: " + (i - 1).toString());
|
|
|
|
// print("categoriesJSON[" + i.toString() + ".toString()] == null");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // does this until error, which then tells it that there is no more JSON left
|
|
|
|
|
|
|
|
// print(categories[11].name);
|
|
|
|
|
|
|
|
// decodedResponse.forEach((key, value) {
|
|
|
|
//// print(key + ": " + value);
|
|
|
|
// categories.add(new Category(name: value, index: key));
|
|
|
|
// });
|
|
|
|
|
|
|
|
// print(categories[10].name.toString()); // prints "Banana"
|
2019-07-19 14:15:04 +00:00
|
|
|
|
|
|
|
return categories; // categories is List<Category>
|
|
|
|
// Category is defined at the top^^
|
2019-07-16 15:55:00 +00:00
|
|
|
} else {
|
|
|
|
// not successful
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|