Created
February 17, 2021 19:46
-
-
Save Abdulsametileri/adb4d831f15ee0820c176e1fb3c1aa03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter/services.dart'; | |
import 'package:flutter_datatable_with_mobx/model/ex_invoice_model.dart'; | |
import 'package:flutter_datatable_with_mobx/model/invoice_model.dart'; | |
import 'package:mobx/mobx.dart'; | |
part 'datatable_view_model.g.dart'; | |
class DataTableViewModel = _DataTableViewModel with _$DataTableViewModel; | |
abstract class _DataTableViewModel with Store { | |
@observable | |
ObservableList<ExInvoiceModel> invoices = <ExInvoiceModel>[].asObservable(); | |
@computed | |
List<ExInvoiceModel> get selectedInvoices => invoices.where((invoice) => invoice.isSelected).toList(); | |
@computed | |
bool get selectedInvoicesIsEmpty => selectedInvoices.isEmpty; | |
@computed | |
int get totalAmount => selectedInvoices.fold(0, (previousValue, element) => previousValue + element.amount); | |
@action | |
Future<void> fetchInvoices() async { | |
var invoicesJson = await rootBundle.loadString('assets/invoices.json'); | |
List<dynamic> decodedJson = jsonDecode(invoicesJson); | |
invoices.addAll(decodedJson.map((e) => ExInvoiceModel(InvoiceModel.fromJson(e))).toList().asObservable()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment