skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution i have done 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'].


  2. 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();
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search