小编Arp*_*sal的帖子

如何解决flutter中'XXXXX类型的对象未在GetIt内部注册'的问题?

我正在尝试使用 get_it 来创建要使用的单例对象。我不希望使用多个连接到 Firebase 的 API 对象。单例对象是 Api 调用 firebase 的对象。

我使用了以下代码

locator.registerLazySingleton<Api>(() => new Api('teams')) ;
Run Code Online (Sandbox Code Playgroud)

当以下代码有效时

locator.registerLazySingleton<TeamViewModel>(() => new TeamViewModel()) ;
Run Code Online (Sandbox Code Playgroud)

Api类的结构如下:

class Api{
  final Firestore _db = Firestore.instance;
  final String path;
  CollectionReference ref;
  
  Api( this.path ) {
    ref = _db.collection(path);
  }

  Future<QuerySnapshot> getDataCollection() {
     return ref.getDocuments() ;
  }
}`
Run Code Online (Sandbox Code Playgroud)

这就是我使用 API 单例对象的方式:

Api _api = locator<Api>();
Run Code Online (Sandbox Code Playgroud)

虽然以下代码工作正常:

Api _api = Api('team');
Run Code Online (Sandbox Code Playgroud)

我在控制台中收到以下错误:

I/flutter (2313):在构建 MultiProvider 时抛出了以下 _Exception:

I/flutter(2313):异常:Api 类型的对象未在 GetIt 内注册

我想知道这是否甚至可能使用 getit 不是解决此问题的正确方法。

android firebase flutter flutter-dependencies

9
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

firebase ×1

flutter ×1

flutter-dependencies ×1