You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

43 lines
1017 B

  1. import 'dart:convert';
  2. import 'package:http/http.dart' as http;
  3. import 'package:get/get.dart';
  4. import 'package:flutter_dotenv/flutter_dotenv.dart';
  5. class StringService {
  6. final String url = dotenv.env['SERVER_URL']! + 'getStrings';
  7. Future<Map<String, String>> fetchStrings() async {
  8. final response = await http.get(Uri.parse(url));
  9. if (response.statusCode == 200) {
  10. Map<String, dynamic> jsonResponse = json.decode(response.body);
  11. return jsonResponse.map((key, value) => MapEntry(key, value.toString()));
  12. } else {
  13. throw Exception('Failed to load strings');
  14. }
  15. }
  16. }
  17. class StringController extends GetxController {
  18. var strings = <String, String>{}.obs;
  19. var isLoading = true.obs;
  20. @override
  21. void onInit() {
  22. fetchStrings();
  23. super.onInit();
  24. }
  25. void fetchStrings() async {
  26. try {
  27. isLoading(true);
  28. var fetchedStrings = await StringService().fetchStrings();
  29. strings.value = fetchedStrings;
  30. } finally {
  31. isLoading(false);
  32. }
  33. }
  34. }