Created
November 6, 2019 00:25
-
-
Save danielsmykowski1/595878cb33e37e0bae52e59f4c654b37 to your computer and use it in GitHub Desktop.
A Flutter survey implementation class for getting random survey questions from firebase firestore and upload survey data to firestore.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:flutter_base/core/apis/interfaces/survey_api.dart'; | |
import 'package:flutter_base/core/models/question.dart'; | |
import 'package:flutter_base/core/models/reply.dart'; | |
class SurveyImpl extends SurveyApi { | |
@override | |
Future<List<Question>> getRandomQuestions(int count) async { | |
List<Question> questions = await Firestore.instance | |
.collection('questions') | |
.getDocuments() | |
.then((QuerySnapshot snapshot) { | |
return snapshot.documents.map((DocumentSnapshot docSnapshot) { | |
Question question = Question.fromMap(docSnapshot.data); | |
question.id = docSnapshot.documentID; | |
return question; | |
}).toList(); | |
}); | |
if (questions.length <= count) { | |
return questions; | |
} | |
List<Question> realQuestions = List<Question>(); | |
for (int i = 0; i < count; i++) { | |
var rng = Random(); | |
int index = rng.nextInt(questions.length); | |
realQuestions.add(questions[index]); | |
questions.removeAt(index); | |
} | |
return realQuestions; | |
} | |
@override | |
Future<void> uploadSurveyData(List<Reply> data) async { | |
try { | |
data.forEach((reply) async { | |
await Firestore.instance | |
.collection('questions/' + reply.questionId + '/replies') | |
.add(reply.toMap()); | |
}); | |
} catch (e, s) { | |
print('uploadSurveyData error: $e, $s'); | |
return Future.error(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment