[英]Performance with injectable, get_it Flutter
I have a question.我有个问题。 In my project, I have some class with singleton.在我的项目中,我有一些单例类。
and init all at $initGetIt the same.并在$initGetIt处初始化所有内容相同。
_i1.GetIt $initGetIt(_i1.GetIt get,
{String? environment, _i2.EnvironmentFilter? environmentFilter}) {
final gh = _i2.GetItHelper(get, environment, environmentFilter);
gh.singleton<_i3.DioConfig>(_i3.DioConfig());
gh.singleton<_i4.DioMethod>(_i4.DioMethod(get<_i3.DioConfig>()));
gh.singleton<_i5.MovieApiProvider>(
_i5.MovieApiProvider(get<_i4.DioMethod>()));
gh.singleton<_i6.Repository>(_i6.Repository());
gh.lazySingleton<_i7.MovieDetailBloc>(
() => _i7.MovieDetailBloc(get<_i6.Repository>()),
dispose: (i) => i.dispose());
gh.lazySingleton<_i8.MoviesBloc>(() => _i8.MoviesBloc(get<_i6.Repository>()),
dispose: (i) => i.dispose());
return get;
}
I think If I scale my project, I can get some problem.我想如果我扩展我的项目,我会遇到一些问题。
Thank you for your anwser!谢谢你的回答!
This function will be too large and we have too much singleton still live in app.这个函数会太大,我们有太多的单例仍然存在于应用程序中。
I have hundreds of GetIt
singletons and I do not see any problems.我有数百个GetIt
单身人士,我没有看到任何问题。
We need to init a lot of Object when opening the app => Can delay app?我们在打开应用程序时需要初始化很多对象 => 可以延迟应用程序吗?
If you are worried about number of Object
s - do not need to worry.如果您担心Object
的数量 - 不必担心。 When Flutter builds a frame (you know, it builds 60 frames per second), it creates a huge widget tree contains thousands of widgets and each widget contains dozens of objects.当 Flutter 构建一帧(你知道,它每秒构建 60 帧)时,它会创建一个巨大的小部件树,其中包含数千个小部件,每个小部件包含数十个对象。 So hundreds of objects are really a very small number .所以几百个对象真的是一个很小的数目。 Dart's Garbage Collector is designed for the many-object case. Dart 的垃圾收集器专为多对象情况而设计。
How to create a singleton only alive when access to the screen and destroyed after pop this screen.如何创建仅在访问屏幕时活着并在弹出此屏幕后销毁的单身人士。
class MyPageOne extends StatefulWidget {
const MyPageOne({Key? key}) : super(key: key);
@override
_MyPageOneState createState() => _MyPageOneState();
}
class _MyPageOneState extends State<MyPageOne> {
@override
void initState() {
super.initState();
GetIt.I.registerSingleton<MyClass>(MyClass());
}
@override
void dispose() {
GetIt.I.unregister<MyClass>();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ...your page...;
}
}
This is just an example and you can, for example, encapsulate it into a widget.这只是一个示例,例如,您可以将其封装到一个小部件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.