gpt4 book ai didi

android - 如何解决flutter中 'Object of type XXXXX is not registered inside GetIt'的问题?

转载 作者:行者123 更新时间:2023-12-03 13:28:16 27 4
gpt4 key购买 nike

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

locator.registerLazySingleton<Api>(() => new Api('teams')) ;
虽然以下代码有效
locator.registerLazySingleton<TeamViewModel>(() => new TeamViewModel()) ;
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() ;
}
}`
这就是我使用 API 单例对象的方式:
Api _api = locator<Api>();
虽然以下代码工作正常:
Api _api = Api('team');
我在控制台中收到以下错误:

I/flutter ( 2313): The following _Exception was thrown building MultiProvider:

I/flutter ( 2313): Exception: Object of type Api is not registered inside GetIt


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

最佳答案

不要忘记在应用程序初始化之前在您的主应用程序文件中调用 setupLocator

  setupLocator();
runApp(MyApp());

关于android - 如何解决flutter中 'Object of type XXXXX is not registered inside GetIt'的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57829323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com