I have this function with firebase but I keep getting the error message stating "Too many positional arguments: 0 expected, but 1 found." with the items in the quotes in the lines with "snapshot.docs[0].data('Quote') as String?"
How can I fix this, any help is appreciated!
Future getNewsPostDetails(String newsPostId) async {
QuerySnapshot snapshot = await FirebaseFirestore.instance
.collection('newsPosts')
.where('newsPostId', isEqualTo: newsPostId)
.get();
newsPostDetails newPostDetails = newsPostDetails(
newsPostTitle: snapshot.docs[0].data('newsPostTitle') as String?,
newsPostAuthor: snapshot.docs[0].data('newsPostAuthor') as String?,
newsPostContent: snapshot.docs[0].data('newsPostContent') as String?,
date: snapshot.docs[0].data('date') as String?,
);
return newPostDetails;
}
Here is the newsPostDetails.dart
class newsPostDetails {
final String? newsPostTitle;
final String? newsPostAuthor;
final String? newsPostContent;
final String? date;
newsPostDetails(
{this.newsPostTitle,
this.newsPostAuthor,
this.newsPostContent,
this.date});
}
CodePudding user response:
The 'data()' function does not expect any arguments. The 'data()' function returns the map back. You can access the information you want from the map.
Future getNewsPostDetails(String newsPostId) async {
QuerySnapshot snapshot = await FirebaseFirestore.instance
.collection('newsPosts')
.where('newsPostId', isEqualTo: newsPostId)
.get();
final newPostData = snapshot.docs[0].data()! as Map<String, dynamic>;
newsPostDetails newPostDetails = newsPostDetails(
newsPostTitle: newPostData['newsPostTitle'] as String?,
newsPostAuthor: newPostData['newsPostAuthor'] as String?,
newsPostContent: newPostData['newsPostContent'] as String?,
date: newPostData['date'] as String?,
);
return newPostDetails;
}