Skip to content

Instantly share code, notes, and snippets.

@Abdulsametileri
Created February 17, 2021 19:46
Show Gist options
  • Save Abdulsametileri/adb4d831f15ee0820c176e1fb3c1aa03 to your computer and use it in GitHub Desktop.
Save Abdulsametileri/adb4d831f15ee0820c176e1fb3c1aa03 to your computer and use it in GitHub Desktop.
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