Trung BìnhFlutter iconFlutter

Giải thích sự khác nhau giữa singleton và factory pattern trong Flutter.

Singleton tạo một instance cho toàn bộ app: getIt.registerSingleton<Repository>(Repository()).

  • Dùng cho tài nguyên chia sẻ (database, API client, repository).
  • Factory tạo instance mới mỗi lần: getIt.registerFactory<UserBloc>(() => UserBloc(repo)).
  • Dùng cho BLoC (mỗi màn hình cần state độc lập).
  • Dùng sai gây state leak (chia sẻ state có thể thay đổi) hoặc lãng phí bộ nhớ.

Xem toàn bộ Flutter cùng filter theo level & chủ đề con.

Mở danh sách Flutter