Flutter 无法在定位器服务中注册模型

Flutter 无法在定位器服务中注册模型,flutter,scoped-model,Flutter,Scoped Model,我试图在定位器中注册一个模型,但当我在模型中输入参数时,我变得未定义。服务定位器 可以使用{}将构造函数参数更改为可选 AddCash(this.id, this.name, this.amount, this.date, this.frequency, this.isDeleted); 到 代码片段 import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; GetIt getIt = GetIt

我试图在定位器中注册一个模型,但当我在模型中输入参数时,我变得未定义。服务定位器


可以使用{}将构造函数参数更改为可选

AddCash(this.id, this.name, this.amount, this.date, this.frequency, this.isDeleted);

代码片段

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;
  }*/
}
您需要在AddCashid、name、amount、data、frequency和isDeleted中传递值。
AddCash(this.id, this.name, this.amount, this.date, this.frequency, this.isDeleted);
AddCash({this.id, this.name, this.amount, this.date, this.frequency, this.isDeleted});
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;
  }*/
}