Reputation: 183
So I'm writing an quiz app and I need to get defined amount of questions from firestore. So i created two files data provider file and repository file, and that's how they look: Data Provider
class TestProvider {
FirebaseFirestore _firestore = FirebaseFirestore.instance;
Future<Map> getQuestionFromFirebase(String documentId) async {
Map mappedQuestion;
await _firestore
.collection('questions')
.doc(documentId)
.get()
.then((DocumentSnapshot snapshot) => mappedQuestion = snapshot.data());
return mappedQuestion;
}
}
Repository
class TestRepository {
final int amountOfQuestions;
TestRepository({
@required this.amountOfQuestions,
});
TestProvider _testProvider;
Future listOfQuestions() async {
List<int> range = numberInRange(amountOfQuestions);
List<Question> listOfQuestions;
for (int i = 1; i <= amountOfQuestions; i++) {
listOfQuestions.add(Question.fromMap(
await _testProvider.getQuestionFromFirebase(range[i].toString())));
}
return listOfQuestions;
}
}
The error i get:
E/flutter ( 5186): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'getQuestionFromFirebase' was called on null.
E/flutter ( 5186): Receiver: null
E/flutter ( 5186): Tried calling: getQuestionFromFirebase("3")
The funny thing about it is the fact that when i call fuction from provider i don't get error. Problem begins when I'm using it from repo class. Any idea how to fix it? I don't want to use FutureBuilder any time I want to use it in widget. I want to transform snapshot into my question model.
Upvotes: 0
Views: 34
Reputation: 4741
In your TestRepository
code it's not clear where you are instantiating TestProvider _testProvider;
member.
The error message is clear The method 'getQuestionFromFirebase' was called on null. So there is no instance of TestProvider
class and _testProvider
is a null pointer.
Grant TestProvider _testProvider = TestProvider();
in your code as said by @ikerfah and you will have no issues in your code.
Upvotes: 1