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

Time:10-30

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;
                          }
                        }
                      }

CodePudding user response:

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

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