Unable to retrieve data field value from Firestore collection

75 Views Asked by At

I try to retrieve the imageLink saved in a Firestore collection of userProfile, but I can't retrieve the data and it displays the following errors. Does anyone know why this happens and how to fix it?

Error

The following _TypeError was thrown building StreamBuilder<DocumentSnapshot<Object?>>(dirty, state:
    _StreamBuilderBaseState<DocumentSnapshot<Object?>, AsyncSnapshot<DocumentSnapshot<Object?>>>#dbdf2):
    type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast**
    
The relevant error-causing widget was:
  StreamBuilder<DocumentSnapshot<Object?>>
  StreamBuilder:file:///C:/Users/project/lib/views/profile_page.dart:173:34

    When the exception was thrown, this was the stack:
    #0      _ProfilePageState.build.<anonymous closure>.<anonymous closure> (package:success_auto/views/profile_page.dart:181:39)
    profile_page.dart:181
    #1      StreamBuilder.build (package:flutter/src/widgets/async.dart:439:81)
    async.dart:439
    #2      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:120:48)
    async.dart:120
    #3      StatefulElement.build (package:flutter/src/widgets/framework.dart:5409:27)
    framework.dart:5409
    #4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5297:15)
    framework.dart:5297
    #5      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5462:11)
    framework.dart:5462
    #6      Element.rebuild (package:flutter/src/widgets/framework.dart:5016:7)
    framework.dart:5016
    #7      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2779:19)
    framework.dart:2779
    #8      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:916:21)
    binding.dart:916
    #9      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:360:5)
    binding.dart:360
    #10     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1297:15)
    binding.dart:1297
    #11     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1227:9)
    binding.dart:1227
    #12     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1085:5)
    binding.dart:1085
    #13     _invoke (dart:ui/hooks.dart:170:13)
    hooks.dart:170
    #14     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:401:5)
    platform_dispatcher.dart:401
    #15     _drawFrame (dart:ui/hooks.dart:140:31)
    hooks.dart:140

UI

[![Firebase Firestore][2]][2]

2

There are 2 best solutions below

0
shaik shaheera On

Make sure that the document with the specified ID exists in the Firestore collection.

Before accessing any field, check if the document exists and the field is not null to avoid the error you mentioned.

And you are using "snapshot" identifier for collection("Users") and collection("userProfile").Try to use different identifiers and try again.

0
MendelG On

Your'e using the null operator (!) in your code:

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

And:

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

The ! is the issue if snapshot.data is null. The ! operator is a null assertion operator in Dart, which tells the compiler that you are certain the value is not null. However, in this case it is.

Instead, what you can use is a ? -- which tells that it can be null:

if (snapshot.hasData && snapshot.data?.data() != null) {
    final userData = snapshot.data!.data() as Map<String, dynamic>;
    // Rest of your code
} else {
    // Handle the situation when snapshot.data is null
    // and investigate your DB.
}

or maybe even instead:

final userData = snapshot.data?.data() as Map<String, dynamic>?;

With a ? instead of !.

See also