Flutter DocumentSnapshot - Error: A value of type 'Object?' can't be assigned to a variable of type 'DocumentSnapshot'

1,345

Do DocumentSnapshot snap = snapshot.data as DocumentSnapshot;

Share:
1,345
han da
Author by

han da

Updated on December 29, 2022

Comments

  • han da
    han da over 1 year
    return StreamBuilder(
      stream: usersRef.doc(post.userId).snapshots(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          DocumentSnapshot snap = snapshot.data;
          UserModel user = UserModel.fromJson(snap.data()!);
    

    Hello, all! The 'snapshot.data' is giving me the above error. Please, help! Thanks!

    • Nikhil Badyal
      Nikhil Badyal about 3 years
      Write like this DocumentSnapshot snap = snapshot.data as DocumentSnapshot ;
    • han da
      han da about 3 years
      oh... that was easy! lol thanks, @Problematic Dude!
    • Nikhil Badyal
      Nikhil Badyal about 3 years
      does it Worked ?
    • han da
      han da about 3 years
      the error is gone. but am stuck on a different issue. not yet checked... but, thanks anyways!
    • Nikhil Badyal
      Nikhil Badyal about 3 years
      In that case I'm adding above comment as an answer. Mark it if error in the question is gone