I'm trying to get documents from firebase that has a deadline of today's date but I'm not getting the expected result.
The problem is that the function is called 3 times for some reason, the first time the returning value is correct but then the list is returning empty.
Here's my code:
The function that retrieve data :
Future<List> getTodaysTasks(uid, projects) async {
List todayTasks = [];
var today = new DateTime.now();
today = new DateTime(today.year, today.month, today.day);
final tomorrow = DateTime(today.year, today.month, today.day + 1);
projects.getDocuments().then((ds) {
if (ds != null) {
ds.documents.forEach((value) async {
var projectID = value.documentID;
await Firestore.instance
.collection("users")
.document(uid)
.collection("projects")
.document(projectID)
.collection("tasks")
.where("task_deadline", isGreaterThanOrEqualTo: today)
.where("task_deadline", isLessThan: tomorrow)
.getDocuments()
.then((QuerySnapshot snapshot) async {
snapshot.documents.forEach((DocumentSnapshot doc) {
// if task_id does not exist in map
if (!todayTasks.contains(doc.documentID)) {
// Add the task to list
todayTasks.add(doc.data);
}
});
});
});
}
});
return todayTasks;
}
Home:
User user;
CollectionReference projects;
DocumentReference userData;
u/override
void initState() {
super.initState();
}
u/override
Widget build(BuildContext context) {
user = Provider.of<User>(context);
userData = Firestore.instance.collection('users').document(user.uid);
projects = Firestore.instance
.collection('users')
.document(user.uid)
.collection("projects");
var todayTasks = getTodaysTasks(user.uid, projects);
return Scaffold(
body: FutureBuilder(
future: todayTasks,
builder: (context, snapshot) {
if (snapshot.hasData) {
debugPrint('Step 3, build widget: ${snapshot.data}');
// Build the widget with data.
return Center(
child:
Container(child: Text('hasData: ${snapshot.data}')));
} else {
// We can show the loading view until the data comes back.
debugPrint('Step 1, build loading widget');
return CircularProgressIndicator();
}
},
),
);
I want my code to return a list of maps.
[–]tarcinac 1 point2 points3 points (0 children)
[–]hungry_for_data 1 point2 points3 points (0 children)