Your Location is: Home > Flutter
Extend a Widget called from another file
Question
Good evening everyone, in practice I created a file containing some functions that are based on firestore that I have to reuse in other files, this is the code:
import 'dart:math';
import 'dart:async';
import 'package:cron/cron.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:provider/provider.dart';
import 'package:goodgoals_android/services/authentication.dart';
abstract class Service {
Future<void> NumberGenerator();
Future<void> MissionRetriever1();
Future<void> MissionRetriever2();
Future<void> MissionRetriever3();
Future<void> MissionRetriever4();
}
class MissionService implements Service {
MissionService({this.auth});
int missionNumber1;
int missionNumber2;
int missionNumber3;
int missionNumber4;
int test = 10;
final BaseAuth auth;
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final databaseReference = FirebaseFirestore.instance;
Future<void> NumberGenerator() {
final cron= Cron();
cron.schedule(Schedule.parse('*0**1'), () async { //Tutti i Lunedì alle 00;
Random random = new Random();
missionNumber1 = random.nextInt(10);
missionNumber2 = random.nextInt(10);
missionNumber3 = random.nextInt(10);
missionNumber4 = random.nextInt(10);
});
}
Future<void> MissionRetriever1() async{
await FirebaseFirestore.instance
.collection("quest")
.doc("1")
.get();
}
Future<void> MissionRetriever2() async{
return await FirebaseFirestore.instance
.collection("quest")
.doc(missionNumber2.toString())
.get();
}
Future<void> MissionRetriever3() async{
return await FirebaseFirestore.instance
.collection("quest")
.doc(missionNumber3.toString())
.get();
}
Future<void> MissionRetriever4() async{
return await FirebaseFirestore.instance
.collection("quest")
.doc(missionNumber4.toString())
.get();
}
}
In practice I would need to call the MissionRetriever1 function in another file, and then save the values with then like this :
Future<void> getMission1() async{
return await widget.mservice.MissionRetriever2
.then((QuerySnapshot querySnapshot) => {
querySnapshot.docs.forEach((doc) {
print("Done");
setState(() {
MissionData1.add(doc["Name"]);
});
}),
});
}
But doing so gives me the error: The method 'then' isn't defined for the type 'Function'.
Any advise?
Best answer
You were missing the () after MissionRetriever2. Your IDE likely didn't warn you because you had everything uppercase which is standard for class names, but not functions. Functions should follow camelcase ex: missionRetriever2
Future<void> getMission1() async{
return await widget.mservice.MissionRetriever2()
.then((QuerySnapshot querySnapshot) => {
querySnapshot.docs.forEach((doc) {
print("Done");
setState(() {
MissionData1.add(doc["Name"]);
});
}),
});
}
Note you also don't need to combine the usage of await & .then() callbacks a cleaner way to do this would be:
Future<void> getMission1() async{
var snap = await widget.mService.missionRetriever2();
for(var doc in snap.docs){
missionData1.add(doc.data()['Name']);
}
}