How to wait for a Firestore document to be created?

I am debugging a piece of code that creates a user in Firebase, and then waits for a document to be created in Firestore by our backend systems. As I scale the process to create more users, it times out and I discovered it is due to this code:

final cred = await FirebaseAuth.instance.signInAnonymously();
final user = cred.user!;
Future.delayed(const Duration(seconds: 15);
final doc = await FirebaseFirestore.instance.doc(user.uid).getDoc();

The 15 second time-out in here is to ensure the document has been created before we try to read it. This may take some time, but usually is much faster than the 15 second limit we use here.

Is there a way to wait for the document without using such a hard-coded time-out?

Recognized by Google Cloud Collective

Hard-coded delays such as this lead to all kinds of unnecessary delays as you try to scale out your code base.

A better approach is to use Firestore’s built-in realtime listeners, which allow you wait exactly until the document has been created – and no longer.

In its simplest form, a realtime listener looks like this:

final uid = FirebaseAuth.instance.currentUser!.uid;
final ref = FirebaseFirestore.instance.doc('users/${uid}')
ref.snapshots().listen((snapshot) {
  if (snapshot.exists) {
    debugPrint(snapshot.data());
  }
});

This prints the data as soon as the document is created.

To wire this up in a UI, you can use a StreamBuilder like this:

StreamBuilder<DocumentSnapshot>(
  stream: ref.snapshots(),
  builder: (_, snapshot) {
    if (snapshot.hasError) return Text('Error listening document: ${snapshot.error}');
    if (!snapshot.hasData) return Text('No user doc');
    final doc = snapshot.data!;
    return Text('Current doc: ${doc.data()}');
  }
)

In the use-case here, we want to use await to wait for the document to appear. This means that we need to convert from the snapshots() Stream into a Future, which we can do with this helper function:

Future<DocumentSnapshot> waitForDocument(DocumentReference ref) {
  final completer = new Completer<DocumentSnapshot>();

  StreamSubscription? sub;
  try {
    sub = ref.snapshots().listen((snapshot) {
      if (snapshot.exists) {
        completer.complete(snapshot);
        sub!.cancel();
      }
    });
  }
  catch (e) {
    completer.completeError(e);
  }

  return completer.future;
}

In here:

  • The Completer is an object that allows you to build your own Future.
  • We need to cancel the listener once the document has been created, so we track the subscription (in sub).

Now with this, we can wait for our document to appear without a hard-coded delay:

final cred = await FirebaseAuth.instance.signInAnonymously();
final user = cred.user!;
final doc = await waitForDocument(FirebaseFirestore.instance.doc(user.uid));

This is missing the 15s timeout that the original code had. If you want to add that, you’ll need to implement a timeout.

You can find a working copy of this code and a (much shorter) explanation on https://zapp.run/edit/firestore-wait-for-doc-creation-zx5k06w7x5l0.

Recognized by Google Cloud Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật