I have a stream provider that depends on a request to resolve. However the execution steps put at the first await
statement, causing the response to be null. Which throws an error
The request provider
final postProvider = FutureProvider.family.autoDispose(
(ref, Tuple2<String?, Map<dynamic, dynamic>?> info) async {
var path = info.item1 ?? '';
var payload = info.item2;
final url = backendUrl().replace(path: path);
return await http.post(
url,
headers: {
'X-FIREBASE_TOKEN':
await ref.read(authProvider).currentUser!.getIdToken() //this is where execution steps out
},
body: payload != null ? jsonEncode(payload) : null,
);
},
);
the StreamProvider:
final chatProvider = StreamProvider.autoDispose((ref) {
final chatId = ref.watch(chatIdProvider);
if (chatId == null) {
final resp = ref
.watch(postProvider(const Tuple2('create-chat', null)))
.value!; //this evaluates to null.
ref.read(chatIdProvider.notifier).state = jsonDecode(resp.body)['chat_id'];
}
final uid = ref.watch(userStreamProvider).value!.uid;
return ref.read(docRefProvider('${uid}_chats/$chatId')).snapshots();
});
I can't use the future from the FutureProvider because that would require me to have an async
callback, which would not return a Future rather than a Stream. How do I work around this?