Refactor environment variable handling and update build instructions
- Introduced a Makefile for building the release APK with `make build release`. - Updated README to reflect new build command and usage of `--dart-define-from-file=.env`. - Removed dotenv dependency and switched to using Dart's `--dart-define` for API key management. - Adjusted ForexRateAPI key loading logic to improve error handling and clarity. Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:rental_income_tracker/screens/home_screen.dart';
|
||||
import 'package:rental_income_tracker/state/rent_controller.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await dotenv.load(fileName: '.env');
|
||||
runApp(const RentalIncomeTrackerApp());
|
||||
}
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'ForexRateAPI key is loaded from .env (FOREX_RATE_API_KEY).',
|
||||
'ForexRateAPI key is loaded from FOREX_RATE_API_KEY via --dart-define-from-file=.env.',
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -41,7 +41,7 @@ class ForexRateApiService {
|
||||
final parsed = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final error = parsed['error'] as Map<String, dynamic>?;
|
||||
final code = error?['code']?.toString().toLowerCase();
|
||||
return code == 'rate_limit_reached';
|
||||
return code == '104' || code == 'rate_limit_reached';
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
@@ -136,6 +136,62 @@ class ForexRateApiService {
|
||||
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
|
||||
throw StateError('ForexRateAPI key is not configured.');
|
||||
}
|
||||
return null;
|
||||
|
||||
final start = DateTime(year, 1, 1);
|
||||
final end = DateTime(year, 12, 31);
|
||||
final uri =
|
||||
Uri.https('api.forexrateapi.com', '/v1/timeframe', <String, String>{
|
||||
'api_key': apiKey,
|
||||
'base': 'USD',
|
||||
'currencies': 'SEK',
|
||||
'start_date': DateFormat('yyyy-MM-dd').format(start),
|
||||
'end_date': DateFormat('yyyy-MM-dd').format(end),
|
||||
});
|
||||
|
||||
_log('Timeframe request: ${_redactedUri(uri)}');
|
||||
|
||||
final response = await _getWithRateLimitRetry(uri, operation: 'Timeframe');
|
||||
if (response.statusCode != 200) {
|
||||
_log(
|
||||
'Timeframe endpoint unavailable for this account or request; falling back to per-day requests. status=${response.statusCode}',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final success = json['success'] as bool? ?? false;
|
||||
if (!success) {
|
||||
_log(
|
||||
'Timeframe success=false; falling back to per-day requests. error=${json['error']}',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
final rates = json['rates'] as Map<String, dynamic>?;
|
||||
if (rates == null) {
|
||||
_log(
|
||||
'Timeframe response missing rates; falling back to per-day requests.',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
final results = <String, double>{};
|
||||
for (final entry in rates.entries) {
|
||||
final dayRates = entry.value as Map<String, dynamic>?;
|
||||
final sek = dayRates?['SEK'];
|
||||
if (sek is num) {
|
||||
results[entry.key] = sek.toDouble();
|
||||
}
|
||||
}
|
||||
|
||||
if (results.isEmpty) {
|
||||
_log(
|
||||
'Timeframe parsed no SEK entries; falling back to per-day requests.',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
_log('Timeframe parsed ${results.length} daily SEK quotes.');
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:rental_income_tracker/models/app_settings.dart';
|
||||
@@ -56,16 +55,17 @@ class RentController extends ChangeNotifier {
|
||||
}
|
||||
|
||||
static String _exchangeRateApiKeyFromEnv() {
|
||||
final key = dotenv.env['FOREX_RATE_API_KEY']?.trim() ?? '';
|
||||
if (key.isEmpty) {
|
||||
const key = String.fromEnvironment('FOREX_RATE_API_KEY');
|
||||
final trimmed = key.trim();
|
||||
if (trimmed.isEmpty) {
|
||||
if (kDebugMode) {
|
||||
debugPrint(
|
||||
'[RentController] FOREX_RATE_API_KEY missing in .env, API calls will fail until set.',
|
||||
'[RentController] FOREX_RATE_API_KEY missing from dart defines. Pass --dart-define-from-file=.env (or --dart-define) when building/running.',
|
||||
);
|
||||
}
|
||||
return 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY';
|
||||
}
|
||||
return key;
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
final StorageService _storageService;
|
||||
|
||||
Reference in New Issue
Block a user