Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A value of type 'Object?' can't be assigned to a variable of type 'List<Cita>

Error: A value of type ‘Object?’ can’t be assigned to a variable of type ‘List’.
Try changing the type of the variable, or casting the right-hand type to ‘List’.

                      class _CitaListState extends State<CitaList> {
                        @override
                        Widget build(BuildContext context) {
                          return FutureBuilder(
                              future:
                                  



                CitaService().getByEmail(FirebaseAuth.instance.currentUser!.email),
                              builder: (context, snapshot) {
                                if (!snapshot.hasData) {
                                  return Container(
                                    child: Text('No tiene citas'),
                                  );
                                }
                                List<Cita> citas = snapshot.data; // Line error


                                return ListView.builder(
                                    itemCount: citas.length,
                                    itemBuilder: (context, index) {
                                      Cita c = citas[index];
                                      return ListTile(
                                        leading: Text(c.turn.toString()),
                                        title: Text(c.formattedDay()),
                                        trailing: c.status == 'cancelled'
                                            ? null
                                            : IconButton(onPressed: () {}, icon: Icon(Icons.delete)),
                                      );
                                    });
                              });
                        }
                      }

getByEmail
here is my getByEmail method which is where I set the values ​​to stores in my database

                      class CitaService {
                        Future<void> create(String? email, DateTime day) async {
                          try {
                            await FirebaseFirestore.instance
                                .collection('citas')
                                .add({'email': email, 'day': day, 'turn': 1, 'status': 'pendiente'});
                          } catch (e) {
                            print(e);
                          }
                        }

                        Future<List<Cita>?> getByEmail(String? email) async {
                          try {
                            var snapshot = await FirebaseFirestore.instance
                                .collection('citas')
                                .where('email', isEqualTo: email)
                                .get();

                            List<Cita> citas = [];
                            snapshot.docs.forEach((element) {
                              citas.add(Cita.fromSnapshot(element));
                            });
                            return citas;
                          } catch (e) {
                            print(e);
                            return null;
                          }
                        }
                      }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try pass List<Cita>? in FutureBuilder's type:

FutureBuilder<List<Cita>?>(//<---add this
    future: CitaService().getByEmail(FirebaseAuth.instance.currentUser!.email),
    builder:...
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading