Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Firebase functionality to save data from a single trial #67

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cognitive_data/databases/firebase_db/firebase_db.dart';
import 'package:cognitive_data/models/device.dart';
import 'package:cognitive_data/models/session.dart';
import 'package:cognitive_data/models/trial.dart';
import 'package:cognitive_data/models/trial_type.dart';
import 'package:flutter/material.dart';

import 'package:firebase_core/firebase_core.dart';
Expand Down Expand Up @@ -34,7 +36,7 @@ class MyHomePage extends StatelessWidget {
final String title;
final _db = FirebaseDB(
FirebaseFirestore.instance,
participantID: '101,',
participantID: '101',
sessionID: '001',
taskName: 'dsb',
);
Expand Down Expand Up @@ -74,7 +76,17 @@ class MyHomePage extends StatelessWidget {
child: const Text("Save Device metadata to firebase"),
),
ElevatedButton(
onPressed: () {},
onPressed: () async {
final Trial trial = Trial(
participantID: _db.participantID,
sessionID: _db.sessionID,
trialType: TrialType.practice,
stim: '123',
response: '321',
);

await _db.addTrial(trial: trial);
},
child: const Text("Save trial to firebase"),
),
ElevatedButton(
Expand Down
18 changes: 16 additions & 2 deletions lib/databases/firebase_db/firebase_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,23 @@ class FirebaseDB implements DB {
await sessionRef.doc('sessionMetadata').set(sessionData);
}

/// Adds a single [trial] to [FirebaseFirestore].
/// Stores each [trial] data in an independent doc inside a collection
/// named `trials`.
@override
void addTrial({required Trial trial}) {
// TODO: implement addTrial
Future<void> addTrial({required Trial trial}) async {
final Map<String, dynamic> trialMap = {
'participantID': trial.participantID,
'sessionID': trial.sessionID,
'trialType': trial.trialType,
'stim': trial.stim,
'response': trial.response,
};

final CollectionReference trialsRef = _db.collection(
'participants/$participantID/cognitive_tasks/$taskName/sessions/$sessionID/trials');

await trialsRef.add(trialMap);
}

@override
Expand Down
Loading