I'm trying to register a model in my locator but I'm getting undefined when I put in the parameters in the model. service_locator.dart
import 'package:get_it/get_it.dart';
import '/services/repository_service.dart';
import '/models/addcash.dart';
GetIt locator = GetIt.instance;
void setupLocator() {
// // Register services
locator.registerLazySingleton<RepositoryServiceAddCash>(
() => RepositoryServiceAddCash());
// Register models
locator.registerFactory<AddCash>(
() => AddCash(id, name, amount, data, frequency, isDeleted));
}
The parameters id
, name
, amount
, data
, frequency
, isDeleted
is coming up as undefined.
This is my model file
import 'package:scoped_model/scoped_model.dart';
import 'package:cash_on_hand/service_locator.dart';
import 'package:cash_on_hand/services/repository_service.dart';
import '../data/database.dart';
class AddCash extends Model {
RepositoryServiceAddCash storageService = locator<RepositoryServiceAddCash>();
int id;
String name;
int amount;
String date;
String frequency;
bool isDeleted;
AddCash(this.id, this.name, this.amount, this.date, this.frequency,
this.isDeleted);
AddCash.fromJson(Map<String, dynamic> json) {
this.id = json[DatabaseCreator.id];
this.name = json[DatabaseCreator.name];
this.amount = json[DatabaseCreator.amount];
this.date = json[DatabaseCreator.date];
this.frequency = json[DatabaseCreator.frequency];
this.isDeleted = json[DatabaseCreator.isDeleted] == 1;
}
}
You can change constructor parameter to optional with {}
AddCash(this.id, this.name, this.amount, this.date, this.frequency, this.isDeleted);
to
AddCash({this.id, this.name, this.amount, this.date, this.frequency, this.isDeleted});
code snippet
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
GetIt getIt = GetIt.instance;
void main() {
getIt.registerSingleton<Model>(AddCash(),
signalsReady: true);
runApp(MyApp());
}
abstract class Model extends ChangeNotifier {
int get id;
String get name;
int get amount;
String get date;
String get frequency;
bool get isDeleted;
}
class AddCash extends Model {
int id;
String name;
int amount;
String date;
String frequency;
bool isDeleted;
AddCash({this.id, this.name, this.amount, this.date, this.frequency,
this.isDeleted});
/*AddCash.fromJson(Map<String, dynamic> json) {
this.id = json[DatabaseCreator.id];
this.name = json[DatabaseCreator.name];
this.amount = json[DatabaseCreator.amount];
this.date = json[DatabaseCreator.date];
this.frequency = json[DatabaseCreator.frequency];
this.isDeleted = json[DatabaseCreator.isDeleted] == 1;
}*/
}