'Combining Provider with a steam provider not updating

I am working on a technique to determine if some has elevated rights to show an edit icon. I am using Firebase for auth and firestore for back end.

My thoughts were to have the page do a quick check for a record within a certain section that requires the user to be in that section. IE there is a section called /admins/. The rules will only let you read that data if your uid is in that list. I have that working.

So I built a FutureProvider:

    final adminCheckProvider = FutureProvider<bool>((ref) async {
  bool admin = false;
  User? _uid = ref.watch(authStateChangesProvider).value;
  if (_uid != null) {
    print(_uid.uid);
    // Check for Admin Document
    final _fireStore = FirebaseFirestore.instance;
    await _fireStore
        .doc(FireStorePath.admin(_uid.uid))
        .get()
        .then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        print('Document Exists: ${documentSnapshot.exists}');
        return true;
      }
    });
  }
  return false;
});

and have a widget that is watching this provider.

    class AdminEdit extends ConsumerWidget {
  const AdminEdit({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    AsyncValue<bool> isAdmin = ref.watch(adminCheckProvider);
    return isAdmin.when(
      data: (isAdmin) {
        print('Data Is In: $isAdmin');
        if (isAdmin) {
          return Text('Is An Admin');
        }
        return Text('Is Not Admin');
      },
      loading: () => Text('Is Not Admin'),
      error: (error, stackTrace) => Text('Error: $error'),
    );
  }
}

I am seeing that the call originally returns false, but when the data is returned and it is determined the document does exist but it never sends out the all clear. I have tried this several different ways and have yet to have this return true. Here is some terminal out put

Data Is In: false
<<UID>>
Document Exists: true
Data Is In: false

If my approach to this is wrong I wouldn't mind hearing about that either.

Thanks in advance guys!



Solution 1:[1]

Looks like you are returning false as default and only returning true inside your future, not the actual FutureProvider.

Try something like this and it should work:

final adminCheckProvider = FutureProvider<bool>((ref) async {
  User? _uid = ref.watch(authStateChangesProvider).value;
  if (_uid != null) {
    print(_uid.uid);
    // Check for Admin Document
    final _fireStore = FirebaseFirestore.instance;
    final doc = await _fireStore.doc(FireStorePath.admin(_uid.uid)).get();
    return doc.exists;
  } else {
    return false;
  }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Josteve