Robo-advisory-dart/lib/providers/transaction_provider.dart

37 lines
1 KiB
Dart
Raw Normal View History

import 'package:flutter/foundation.dart';
import 'package:robo_advisory/models/transaction.dart';
import 'package:robo_advisory/repository/transaction.repository.dart';
class TransactionProvider with ChangeNotifier {
2022-09-11 14:37:28 +00:00
String? _selectedCategory;
List<Transaction> _transactionsData = [];
List<Transaction> get transactionList {
2022-09-11 14:37:28 +00:00
if (_selectedCategory == null) {
return _transactionsData;
}
2022-09-11 14:37:28 +00:00
return _transactionsData
.where((transaction) =>
transaction.category.name == _selectedCategory?.toLowerCase())
.toList();
}
void setTransactionsList(List<Transaction> transactions) {
_transactionsData = transactions;
notifyListeners();
}
2022-09-11 14:37:28 +00:00
String? get selectedCategory => _selectedCategory;
2022-09-11 14:37:28 +00:00
void setSelectedCategory(String? category) {
_selectedCategory = category;
notifyListeners();
}
Future<List<Transaction>> loadTransactions() async {
return (await TransactionRepository.loadTransactions() as List)
.map((i) => Transaction.fromJson(i))
.toList();
}
}