简体   繁体   中英

Flutter StreamBuilder error type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast

I create a document when a user signs in and after it's created I am going to another page where the document data should be read. The error happens only the first time when the document is created, later when the user logs out and signs back in, the document exists and I continue the flow to the same point where I read data in a stream.

The first time when the document is read and the error pops up in console, it also shows for a noticeable moment on the device, and goes away.

The error is:

The following _CastError was thrown building StreamBuilder<DocumentSnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot<Object?>, AsyncSnapshot<DocumentSnapshot<Object?>>>#8f55f):
type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast

And this is the relevant StreamBuilder where it happens.

StreamBuilder<DocumentSnapshot>(
              stream: database.provideDocFieldStream(auth.currentUser!.uid),
              builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                if (snapshot.hasData) {
                  Map<String, dynamic> documentFields = snapshot.data!.data() as Map<String, dynamic>;
                  return Column(
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            documentFields['fullName'],
                            style: Theme.of(context).textTheme.overline,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ],
                      ),
                      Row(
                        children: [
                          Text(
                            "My points: ",
                            style: Theme.of(context).textTheme.subtitle2,
                          ),
                          Text(
                            documentFields['totalPoints'].toString(),
                            style: Theme.of(context).textTheme.subtitle2,
                          ),
                        ],
                      )
                    ],
                  );
                }

The stack points to this part specifically

Map<String, dynamic> documentFields = snapshot.data!.data() as Map<String, dynamic>;

I'm at my wits end here, what is actually going on? What am I doing wrong?

Thanks!

You can only get the data from a DocumentSnapshot if that document exists, and you code is not handling that case yet.

if (snapshot.hasData && snapshot.data.exists) {
  Map<String, dynamic> documentFields = snapshot.data!.data() as Map<String, dynamic>;

Also see the FlutterFire documentation onreading data from Firestore , which has a better example of handling all the states.

You should also write what happens if the snapshot doesn't have data. Provide an else statement.

You should make an if-else statement like this - if the snapshot doesn't have data it will show a CIRCULARPROGRESSINDICATOR else it will show the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM