Home > Blockchain >  The argument type 'Object?' can't be assigned to the parameter type 'String?
The argument type 'Object?' can't be assigned to the parameter type 'String?

Time:02-11

I get this error with my function, "The argument type 'Object?' can't be assigned to the parameter type 'String?'." on the lines containing snapshot.docs[0].data(''),

Please help me fix it, any help would be greatly appreciated!

Future getNewsPostDetails(String newsPostId) async {

    QuerySnapshot snapshot = await FirebaseFirestore.instance.collection('newsPosts').where('newsPostId', isEqualTo: newsPostId).get();
      newsPostDetails blogPostDetails = newsPostDetails(
      newsPostTitle: snapshot.docs[0].data('newsPostTitle'),
      newsPostAuthor: snapshot.docs[0].data('newsPostAuthor'),
      newsPostContent: snapshot.docs[0].data('newsPostContent') ,
      date: snapshot.docs[0].data('date'),
    );
    return blogPostDetails;
  }

CodePudding user response:

If you make sure that snapshot.docs[0].data('newsPostTitle') is a String, you can convert it to String like:

snapshot.docs[0].data('newsPostTitle') as String?
  • Related