Home > Software design >  Deleting a firebase user results in Flutter App having an infinite loading
Deleting a firebase user results in Flutter App having an infinite loading

Time:04-12

I am building a flutter firebase app that requires a user to sign in. My app has a homepage that renders only when the user data has been fetched. If the user is not completely fetched, it returns a loading widget.

    return StreamBuilder<UserData>(
    stream: DatabaseService(uid: uid).userData,
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        UserData? userData = snapshot.data;
        return Scaffold(...);
    }else {
      return LoadingWidget();
     }

This works well if the user signs out and back in with the loading only showing for a split second. However, if a user is deleted while they are signed in, the Loading screen keeps going. Also, If I change the firestore document of a user and remove one of the fields of the UserData Model, the infinitely Loading Screen shows. Anyone know how to resolve this?

Thanks.

CodePudding user response:

Your code tries to load the data for a user, and if there is no data (yet) it shows a LoadingWidget. This is working as intended if the data can be loaded, but if there is no data to load (or no current user to load data for) you permanently render the LoadingWidget.

You'll want to distinguish between those two cases, and possibly more of them. A simple way:

builder: (context, snapshot) {
  if (snapshot.hasData) {
    UserData? userData = snapshot.data;
    return Scaffold(...);
  } else if (snapshot.hasError) {
    return Text('Error loading user data: ${snapshot.error}');
  } else {
    return LoadingWidget();
  }

I recommend checking the documentation and example for the StreamBuilder widget as its code shows how to handle all cases.

  • Related