Home > Net >  _TypeError (type 'Null' is not a subtype of type 'FutureOr<String>')
_TypeError (type 'Null' is not a subtype of type 'FutureOr<String>')

Time:11-14

I am trying to fetch profile image from firestore. But it is giving an error. Here is the code of fuction which is use to get the image from database. Kindly help if you can

Future<String> getUserImage() async {
  final uid = auth.currentUser?.uid;
  final users = await firestore
     .collection("app")
     .doc("user")
     .collection("driver")
     .doc(uid)
     .get();
  return users.data()?['dp'];
}

CodePudding user response:

Your getUserImage method cant return null, you can return default value return users.get('dp')?? "got null";

or accept nullable data

Future<String?> getUserImage() async {
  final uid = auth.currentUser?.uid;
  final users = await firestore
     .collection("app")
     .doc("user")
     .collection("driver")
     .doc(uid)
     .get();
  return users.get('dp');
}

CodePudding user response:

Try the following code:

Future<String> getUserImage() async {
  final String uid = auth.currentUser!.uid;
  final DocumentSnapshot<Map<String, dynamic>> users = await firestore
      .collection("app")
      .doc("user")
      .collection("driver")
      .doc(uid)
      .get();
  return users.get('dp');
}
  • Related