Home > Enterprise >  I'm getting an error when pulling information from Firebase Firestore
I'm getting an error when pulling information from Firebase Firestore

Time:04-08

I have a database with user information through Firebase Firestore. I put the person's name, email address and link to their profile photo in this database. The problem I'm having is not being able to embed the profile photo into the app.

Codes:

FutureBuilder<String>(
  future: getImage(),
  builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
    if(snapshot.hasData)
      return Image.network(snapshot.data);
    }
  ),

getImage:

  Future <String> getImage() async {
    var uID = FirebaseAuth.instance.currentUser.uid;
    FirebaseFirestore.instance.collection('users').doc(uID).get().then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        print(documentSnapshot["profilFoto"]);
        return ["profilFoto"].toString();
      } else {
        print("Profile photo is not found.");
        return "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/No-Symbol.svg/2048px-No-Symbol.svg.png"; // None "X" image
      }
    });
  }

In the console, I get the following output:

I/flutter (18960): https://firebasestorage.googleapis.com/v0/b/simtotodolist.appspot.com/o/defaultProfilePhoto.png?alt=media&token=f1affb15-dc94-4f1a-a624-c1db65e77735

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building MainScreen(dirty, state: _MainScreenState#be58c):
Row's children must not contain any null values, but a null value was found at index 0

The relevant error-causing widget was
MainScreen
When the exception was thrown, this was the stack
#0      new MultiChildRenderObjectWidget.<anonymous closure>
#1      new MultiChildRenderObjectWidget
#2      new Flex
#3      new Row
#4      _MainScreenState.build
#5      StatefulElement.build
#6      ComponentElement.performRebuild
#7      StatefulElement.performRebuild
#8      Element.rebuild
#9      ComponentElement._firstBuild
#10     StatefulElement._firstBuild
#11     ComponentElement.mount
...     Normal element mounting (190 frames)
#201    Element.inflateWidget
#202    MultiChildRenderObjectElement.inflateWidget
#203    MultiChildRenderObjectElement.mount
...     Normal element mounting (126 frames)
#329    Element.inflateWidget
#330    MultiChildRenderObjectElement.inflateWidget
#331    MultiChildRenderObjectElement.mount
...     Normal element mounting (45 frames)
#376    Element.inflateWidget
#377    MultiChildRenderObjectElement.inflateWidget
#378    MultiChildRenderObjectElement.mount
...     Normal element mounting (15 frames)
#393    Element.inflateWidget
#394    MultiChildRenderObjectElement.inflateWidget
#395    MultiChildRenderObjectElement.mount
...     Normal element mounting (206 frames)
#601    Element.inflateWidget
#602    MultiChildRenderObjectElement.inflateWidget
#603    MultiChildRenderObjectElement.mount
...     Normal element mounting (362 frames)
#965    Element.inflateWidget
#966    Element.updateChild
#967    RenderObjectToWidgetElement._rebuild
#968    RenderObjectToWidgetElement.mount
#969    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure>
#970    BuildOwner.buildScope
#971    RenderObjectToWidgetAdapter.attachToRenderTree
#972    WidgetsBinding.attachRootWidget
#973    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure>
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════
Restarted application in 1.800ms.
W/DynamiteModule(18960): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(18960): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(18960): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
W/ProviderInstaller(18960): Failed to report request stats: com.google.android.gms.common.security.ProviderInstallerImpl.reportRequestStats [class android.content.Context, long, long]
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(18960): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
Reloaded 4 of 1285 libraries in 333ms.

If you look at the top of the console output, I can access the URL of the person's profile photo.

Also database:

enter image description here

How can I solve the problem? Thank you very much in advance for the help.


The error I got after doing what @Victor Eronmosele said:

I/flutter (19850): https://firebasestorage.googleapis.com/v0/b/simtotodolist.appspot.com/o/defaultProfilePhoto.png?alt=media&token=f1affb15-dc94-4f1a-a624-c1db65e77735
I/flutter (19850): https://firebasestorage.googleapis.com/v0/b/simtotodolist.appspot.com/o/defaultProfilePhoto.png?alt=media&token=f1affb15-dc94-4f1a-a624-c1db65e77735
D/EGL_emulation(19850): eglMakeCurrent: 0xef57f7c0: ver 2 0 (tinfo 0xc45bec70)

════════ Exception caught by image resource service ════════════════════════════
The following ArgumentError was thrown resolving an image codec:
Invalid argument(s): No host specified in URI file:///null

When the exception was thrown, this was the stack
#0      _HttpClient._openUrl (dart:_http/http_impl.dart:2667:9)
#1      _HttpClient.getUrl (dart:_http/http_impl.dart:2575:48)
#2      NetworkImage._loadAsync
#3      NetworkImage.load
#4      ImageProvider.resolveStreamForKey.<anonymous closure>
#5      ImageCache.putIfAbsent
#6      ImageProvider.resolveStreamForKey
#7      ScrollAwareImageProvider.resolveStreamForKey
#8      ImageProvider.resolve.<anonymous closure>
#9      ImageProvider._createErrorHandlerAndKey.<anonymous closure>.<anonymous closure>
#10     SynchronousFuture.then
#11     ImageProvider._createErrorHandlerAndKey.<anonymous closure>
#15     ImageProvider._createErrorHandlerAndKey
#16     ImageProvider.resolve
#17     _ImageState._resolveImage
#18     _ImageState.didChangeDependencies
#19     StatefulElement._firstBuild
#20     ComponentElement.mount
#21     Element.inflateWidget
#22     MultiChildRenderObjectElement.inflateWidget
#23     MultiChildRenderObjectElement.mount
...     Normal element mounting (4 frames)
#27     Element.inflateWidget
#28     MultiChildRenderObjectElement.inflateWidget
#29     MultiChildRenderObjectElement.mount
...     Normal element mounting (13 frames)
#42     Element.inflateWidget
#43     MultiChildRenderObjectElement.inflateWidget
#44     MultiChildRenderObjectElement.mount
#45     Element.inflateWidget
#46     MultiChildRenderObjectElement.inflateWidget
#47     MultiChildRenderObjectElement.mount
...     Normal element mounting (198 frames)
#245    Element.inflateWidget
#246    MultiChildRenderObjectElement.inflateWidget
#247    MultiChildRenderObjectElement.mount
...     Normal element mounting (126 frames)
#373    Element.inflateWidget
#374    MultiChildRenderObjectElement.inflateWidget
#375    MultiChildRenderObjectElement.mount
...     Normal element mounting (45 frames)
#420    Element.inflateWidget
#421    MultiChildRenderObjectElement.inflateWidget
#422    MultiChildRenderObjectElement.mount
...     Normal element mounting (15 frames)
#437    Element.inflateWidget
#438    MultiChildRenderObjectElement.inflateWidget
#439    MultiChildRenderObjectElement.mount
...     Normal element mounting (206 frames)
#645    Element.inflateWidget
#646    MultiChildRenderObjectElement.inflateWidget
#647    MultiChildRenderObjectElement.mount
...     Normal element mounting (362 frames)
#1009   Element.inflateWidget
#1010   Element.updateChild
#1011   RenderObjectToWidgetElement._rebuild
#1012   RenderObjectToWidgetElement.mount
#1013   RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure>
#1014   BuildOwner.buildScope
#1015   RenderObjectToWidgetAdapter.attachToRenderTree
#1016   WidgetsBinding.attachRootWidget
#1017   WidgetsBinding.scheduleAttachRootWidget.<anonymous closure>
(elided 14 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
Image provider: NetworkImage("null", scale: 1.0)
Image key: NetworkImage("null", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
A RenderFlex overflowed by 154 pixels on the right.
The relevant error-causing widget was
Row
════════════════════════════════════════════════════════════════════════════════
Restarted application in 8.254ms.
W/to.todolist.ap(19850): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
W/DynamiteModule(19850): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(19850): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(19850): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
W/ProviderInstaller(19850): Failed to report request stats: com.google.android.gms.common.security.ProviderInstallerImpl.reportRequestStats [class android.content.Context, long, long]
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/to.todolist.ap(19850): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)

The output I got after the print(snapshot.data) code:

I/flutter ( 5280): [profilFoto]

CodePudding user response:

Problem:

The problem is that you're only returning a widget when you have data in your FutureBuilder, and since the Future getImage() takes some time to get it's data, it means your FutureBuilder will return null.

That is why the error message says:

Row's children must not contain any null values, but a null value was found at index 0

I'm assuming your FutureBuilder is inside a Row, based on this error message.

Solution:

You can solve this by returning a loading indicator when the FutureBuilder does not have any data, that way, the FutureBuider does not return null when there is no data.

Update your FutureBuilder code to this:

FutureBuilder<String>(
  future: getImage(),
  builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
    if(snapshot.hasData) {
      return Image.network(snapshot.data);
    } else {
      return Center(
        child: CircularProgressIndicator()
      );
    }
  }
),

Edit (Second Error Message):

Problem:

Invalid argument(s): No host specified in URI file:///null

The error above is as a result of the getImage method returning null because of how your async code is structured.

Your code completes and returns null before the .then part of the code is run.

Solution:

You can fix the issue by awaiting the document snapshot before returning the value. This pauses the program execution so your FutureBuilder waits for the data to arrive before attempting to display the image.

Update the getImage method to this below:

Future <String> getImage() async {
    var uID = FirebaseAuth.instance.currentUser.uid;
    final DocumentSnapshot documentSnapshot = await FirebaseFirestore.instance.collection('users').doc(uID).get();
    if (documentSnapshot.exists) {
      print(documentSnapshot["profilFoto"]);
      return ["profilFoto"].toString();
    } else {
      print("Profile photo is not found.");
      return "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/No-Symbol.svg/2048px-No-Symbol.svg.png"; // None "X" image
    }
}

More Reading: Checkout the following resources to learn more about FutureBuilder and asynchronous programming in Dart:

  • Related