Services.js
import 'package:flutter/material.dart';
import 'package:showapp/constant/constant.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
class Services {
final api = Constant.API_URL;
loginFormPost(String phone, password, BuildContext context) async {
var url = Uri.parse(api + "/auth");
var response = await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{'phone': phone, 'password': password}),
);
if (response.statusCode == 200) {
if (jsonDecode(response.body)['status'] == 'ok') {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('login', true);
Navigator.pushReplacementNamed(context, "/AdminHomeScreen");
return null;
} else {
return {
'status': jsonDecode(response.body)['status'],
'message': jsonDecode(response.body)['message']
};
}
} else {
return response.statusCode;
}
}
newProductPost(
String title, code, description, category, BuildContext context) async {
try {
var url = Uri.parse(api + "/newProduct");
var response = await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'title': title,
'code': code,
'description': description,
'category': category
}),
);
if (response.statusCode == 200) {
if (jsonDecode(response.body)['status'] == 'ok') {
return {
'status': jsonDecode(response.body)['status'],
'message': jsonDecode(response.body)['message']
};
} else {
return {
'status': jsonDecode(response.body)['status'],
'message': jsonDecode(response.body)['message']
};
}
} else {
return response.statusCode;
}
} catch (e) {
return 'connectionError';
}
}
categoryList() async {
var url = Uri.parse(api + "/categoryList");
List categoryList = [];
var response = await http.get(
url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode == 200) {
if (jsonDecode(response.body) != null) {
if (jsonDecode(response.body)['status'] == 'ok') {
categoryList = jsonDecode(response.body)['message'];
return categoryList;
} else {
return {
'status': jsonDecode(response.body)['status'],
'message': jsonDecode(response.body)['message']
};
}
} else {
throw Exception('error');
}
} else {
throw Exception(
'Failed load data with status code ${response.statusCode}');
}
}
}
categoryList() fonksiyonumun çıktısını dropdown buttonu için alıyorum o çıktı içinde şöyle bir yöntem izledim
categoryList = await services.categoryList();
setState(() {});
}
@override
void initState() {
getCategory();
}
Sizce yazdığım fonksiyonlarda yada bu kategorileri getirdiğim fonksiyonların sonuçlarını daha mantıklı nasıl alabilirim ?
iyi forumlar..