I am building a productivity app using Flutter, Provider, and Firebase. I currently have a number of streams where I take a collection from Firestore and turn them into lists of models. I am running into an issue where when I attempt to create a list of task models, I am returned a null list. Inside Firestore, I have a collection of tasks, statuses, and projects. I would like to set the task.project value equal to the associated Project model from the project collection. main.dart:
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(ProductivityApp());
}
class ProductivityApp extends StatefulWidget {
@override
_ProductivityAppState createState() => _ProductivityAppState();
}
class _ProductivityAppState extends State<ProductivityApp> {
bool _initialized = false;
bool _error = false;
void initializeFlutterFire() async {
try {
await Firebase.initializeApp();
setState(() {
_initialized = true;
});
} catch (e) {
_error = true;
}
}
@override
void initState() {
initializeFlutterFire();
super.initState();
}
@override
Widget build(BuildContext context) {
if (_error) {
return Center(child: Text('Something went wrong'));
}
if (!_initialized) {
return Center(child: Text('Loading'));
}
return StreamProvider<User>.value(
value: AuthService().user,
child: MultiProvider( // TODO: fix the issue for user to be logged in for the first time
providers: [
StreamProvider<List<Project>>.value(value: ProjectService().streamProjects()),
StreamProvider<List<Task>>.value(value: TaskService().streamTasks(context)),
StreamProvider<List<Status>>.value(value: StatusService().streamStatuses())
],
child: MaterialApp(
title: 'ProductivityApp',
theme: appTheme(),
// home: Wrapper(),
// routes: routes,
onGenerateRoute: generateRoute,
initialRoute: '/',
),
),
);
}
}
Task Stream:
// Snapshot Conversion to Task Model and Stream
Stream<List<Task>> streamTasks(BuildContext context) {
var ref = _getTaskReference();
return ref.snapshots().map((querySnapshot) => querySnapshot.docs
.map((queryDocument) => Task.fromFirestore(queryDocument, context))
.toList());
}
Task Model:
class Task {
String taskID = '';
String taskName = 'Default Name';
Project project = Project();
Status status = Status();
int taskTime = 0;
DateTime dueDate = DateTime(1);
DateTime createDate = DateTime.now();
// List<String> subtasks = [];
Task(
{this.taskID,
this.taskName,
this.project,
this.status,
this.taskTime,
this.dueDate,
this.createDate});
factory Task.fromFirestore(DocumentSnapshot snapshot, BuildContext context) {
List<Project> projects = Provider.of<List<Project>>(context);
List<Status> statuses = Provider.of<List<Status>>(context);
Map data = snapshot.data();
Project associatedProject = projects
.firstWhere((project) => project.projectName == data['pojectName']);
print(associatedProject);
Status status = statuses
.firstWhere((status) => status.statusName == data['statusName']);
return Task(
taskID: snapshot.id ?? '',
taskName: data['taskName'].toString() ?? '',
project: associatedProject ?? Project(),
status: status ?? Status(),
taskTime: data['taskTime'] ?? 0,
dueDate:
DateTimeFunctions().timeStampToDateTime(date: data['dueDate']) ??
DateTime.now(),
createDate:
DateTimeFunctions().timeStampToDateTime(date: data['createDate']) ??
DateTime.now());
}
}
Thank you for any help!