I'm trying to figure out what the problem with my StreamBuilder/Stream is:
Stream<Page> pageStream;
@override
void initState() {
super.initState();
final firestoreService = Provider.of<FirestoreService>(context, listen: false);
final book = Provider.of<Book>(context, listen: false);
final pageNumber = Provider.of<int>(context, listen: false);
pageStream = firestoreService.getStreamOfPage(bookId: book.bookId, pageNumber: pageNumber);
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: pageStream,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting ||
snapshot.connectionState == ConnectionState.none) {
return CupertinoActivityIndicator();
}
if (snapshot.hasError) {
return Text('the error is = ${snapshot.error.toString()}');
}
Page page = snapshot.data;
return Container(
color: Colors.purple,
height: 30,
width: 30,
);
}
);
}
So in the initstate method I get the stream which is stored in the state and then assigned to the StreamBuilder.
The method that gets the Stream is:
Stream<Page> getStreamOfPage(
{@required String bookId, @required int pageNumber}) {
try {
Stream<Page> pageStream = _fireStore
.document('books/$bookId/$pageNumber')
.snapshots()
.map((docSnapshot) => Page.fromMap(map: docSnapshot.data));
return pageStream;
} catch (e) {
print(
'Could not get stream of page with bookId = $bookId and pageNumber = $pageNumber');
print(e);
return null;
}
}
The print statements I wrote inside the constructor Page.fromMap() (including one at the end of the constructor) were executed so I assume the error is not there. Print statements I added inside the map method (from .snapshots().map(...)) were not executed, which makes no sense to me because the ones in the Page constructor are executed (with the right values from the firebase document).
The call stack is as follows:
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23c7127e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff513fbb20 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23c70ff8 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff256e9c1a -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 166
4 Runner 0x00000001018ca524 _ZN8firebase9firestore4util16ObjcThrowHandlerENS1_13ExceptionTypeEPKcS4_iRKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEE + 356
5 Runner 0x00000001018c9d63 _ZN8firebase9firestore4util5ThrowENS1_13Except<…>