I am having trouble understanding how to return null using: orElse: () => null
My method is the following:
@override
Future<People> searchPeople({required String email}) async {
var user = auth.FirebaseAuth.instance.currentUser;
final docs = await FirebaseFirestore.instance
.collection('users')
.doc(user!.email)
.collection('people')
.where('hunting', isEqualTo: email)
.get();
final docData = docs.docs.map((doc) {
return People.fromSnapshot(doc);
});
var res = docData.firstWhere(
(element) => element.hunting == email,
orElse: () => null, // The return type 'Null' isn't a 'People', as required by the closure's
);
print(res);
return res;
}
The problem is that it throws the error: "The return type ‘Null’ isn’t a ‘People’, as required by the closure’s"
I have already read many answers here but all examples and answers apply only to return type string, int, etc… How to handle null when a type is an object (People)?
Already tried to use collection: firstWhereOrNull but the error persists…
Is something that I should change in my model?
class People extends Equatable {
String? hunting;
String? username;
String? persona;
People({
this.hunting,
this.username,
this.persona,
});
@override
List<Object?> get props => [hunting, username, persona];
static People fromSnapshot(DocumentSnapshot snapshot) {
People people = People(
hunting: snapshot['hunting'],
username: snapshot['username'],
persona: snapshot['persona'],
);
return people;
}
Map<String, dynamic> toMap() {
return {
'hunter': hunting,
'username': username,
'persona': persona,
};
}
}
Thanks for any help!
2
Answers
Try doing this instead:
The signature for
Iterable<E>.firstWhere
is:That is,
Iterable<E>.firstWhere
must return anE
. It cannot returnE?
. IfE
is non-nullable, then.firstWhere
cannot returnnull
. As explained by the Dart Null Safety FAQ, if you want to returnnull
from.firstWhere
, you instead should use thefirstWhereOrNull
extension method frompackage:collection
.However, your
searchPeople
method is declared to return aFuture<People>
, not aFuture<People?>
. Even if you usefirstWhereOrNull
, yoursearchPeople
function cannot legally returnnull
anyway. You therefore would need to additionally do one of:searchPeople
(and in all of the base classes).null
.