Home > Mobile >  Realtime Update of UI with StreamBuilder and bool -ERROR Expected a value of type 'Map<dynam
Realtime Update of UI with StreamBuilder and bool -ERROR Expected a value of type 'Map<dynam

Time:08-21

In the title I explained what I want to do. I have a bool value named 'turnInvitingPlayer' stored somewhere in a document field in Firestore. The location of the document I know exactly from the instance Variables of GameTable.

This is what i tried:

class GameTable extends StatefulWidget {
  GameTable({Key? key,
    required this.player,
    required this.invitationID,
    required this.invitationIdPlayerInvited,
    required this.invitationIdPlayerInviting})
      : super(key: key);
  final Player? player;
  final String invitationIdPlayerInvited;
  final String invitationIdPlayerInviting;

  /// the invitation ID is the doc name of the gambling Table
  final String invitationID;

  @override
  State<GameTable> createState() => _GameTableState();
}

class _GameTableState extends State<GameTable> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('GameTables')
            .doc(widget.invitationID)
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var dataGameTable = snapshot.data! as Map;
            var turnInvitingPlayer =
            dataGameTable['turnInvitingPlayer'] as bool;
            if (turnInvitingPlayer == true) {
              return Container(color: Colors.blue);
            } else {
              return Container(color: Colors.red);
            }
          } else if (!snapshot.hasData) {
            return Container(
              child: Text('There is no data'),
            );
          }
          return CircularProgressIndicator();
        });
  }
}

I am getting the following error when I run the App Expected a value of type 'Map<dynamic, dynamic>', but got one of type '_JsonDocumentSnapshot' Can somebody show me a way how I can simple access the bool value of the stream and use it in if Clauses?

Thank's to everybody who will help.

CodePudding user response:

I found the solution i have do following changes:

var dataGameTable = snapshot.data!; // remove as Map
var turnInvitingPlayer = dataGameTable['turnInvitingPlayer'] as bool; // remove this line

Now I have access to the boolean value with simple dataGameTable['turnInvitingPlayer'].

CodePudding user response:

Modify your stream builder as follows:

return StreamBuilder<Map<dynamic,dynamic>>(
    stream: FirebaseFirestore.instance
        .collection('GameTables')
        .doc(widget.invitationID)
        .snapshots(),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        var dataGameTable = snapshot.data! as Map;
        var turnInvitingPlayer =
        dataGameTable['turnInvitingPlayer'] as bool;
        if (turnInvitingPlayer == true) {
          return Container(color: Colors.blue);
        } else {
          return Container(color: Colors.red);
        }
      } else if (!snapshot.hasData) {
        return Container(
          child: Text('There is no data'),
        );
      }
      return CircularProgressIndicator();
    });
  • Related