Flutter Provider - how to handle add/edit route. Should it be separate?

Viewed 34

Would someone be able to guide me how to handle add/edit (for example Document object). Should it be a separate route or when "add" then Document.Id = 0, when "edit" then Document.Id != 0. How to pass, some kind of, dynamic parameter to a Provider?

class AddEditDocumentProvider with ChangeNotifier {
  AddEditDocumentProvider(int id) {
    if (id != 0) {
      // fetchFromAPI
      notifyListeners();
    }
  }
  final Document _document = Document(date: DateTime.now(), creationDate: DateTime.now(), paymentDate: DateTime.now());
  Document get document => _document;
}

Init value will be always a new, default Document object, but how to load an object from an API on edit action and change default one?

I assumed that I need to add every operation to this Provider, like:

  1. changing document date, paymentType
  2. add/edit document element - changing quantity and/or price, recalculate net, vat, gross
  3. changing customer - selected customer determines paymentDate and discounts

I am not able to change Document.Id with this approach:

  runApp(
    MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => AddEditDocumentProvider(0)),],
      child: MainApp(),
    ),
  );
0 Answers
Related