0

Until now, I have been uploading my firebase database as follows:

          try {
        _firestore.collection('activity').doc(docID).set({
          DateFormat('MM-dd-yyyy').format(dateTime): {
            'host-$listOfAcceptedPlayers': {
              'levels': [selectedRange.start, selectedRange.end],
              'listOfAcceptedPlayers': [listOfAcceptedPlayers],
              'numOfPlayers': '${_amountOfPlayers[_selectedPlayer]}',
              'time': [
                DateFormat('HHmm').format(dateTime),
                DateFormat('HHmm').format(endDateTime)
              ],
              // 'time_end': '${DateFormat('HHmm').format(endDateTime)}'
            }
          }
        }, SetOptions(merge: true));
      } catch (e) {
        print(e);
      }

Although because I am unable to parse the response from the server since it gives me a non JSON response (no ""), I am trying to update the way I upload my code to the following method:

 var bodyHost = {};
      bodyHost["time"] = [
        "${DateFormat('HHmm').format(dateTime)}",
        "${DateFormat('HHmm').format(endDateTime)}"
      ];
      // var numOfPlayers = {};
      bodyHost["numOfPlayers"] = "${_amountOfPlayers[_selectedPlayer]}";
      // var listOfAcceptedPlayers = {};
      bodyHost["listOfAcceptedPlayers"] = "$listOfAcceptedPlayers";
      // var levels =  {};
      bodyHost["levels"] = [
        "${selectedRange.start}",
        "${selectedRange.end}"
      ];
      var host = {};
      host["host-$listOfAcceptedPlayers"] = bodyHost;
      var date = {};
      date["${DateFormat('MM-dd-yyyy').format(dateTime)}"] = host;
      String str = json.encode(date);
      print(str);
      _firestore.collection('activity').doc(docID).set({str});

Although I am getting the following error The argument type 'Set' can't be assigned to the parameter type 'Map<String, dynamic>'. (Documentation). What is the right way to go about this?

If there is a way to parse the original response please let me know, just one thing to note - the "host-listOfAcceptedPlayers" will change every time I read it, and I want to capture what the listOfAcceptedPlayers is to then pull the data beneath it in the tree. The easiest way would be snapshot.data['levels'] (for example) although I cannot access the tree without knowing the listOfAcceptedPlayers (and how many/what the list of different emails would be).

1 Answer 1

1

I actually found out a different way to do this. Using RegEx I am able to search for my user's email address and return that in the following list:

    var todaySnap =
    snapshot.data['04-24-2023'];
print(todaySnap);
var todaySnapStr =
    todaySnap.toString();
var regexp =
    RegExp(r"(?<=host-).+?(?=:)");
print(regexp.allMatches(
    todaySnapStr, 0));

final emailMatches = regexp
    .allMatches(todaySnapStr)
    .map((m) => m.group(0))
    .toList();
print(emailMatches[1]);

The host- and : are what separate my email address in my specific case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.