Home > OS >  Inserting List of strings as list of objects
Inserting List of strings as list of objects

Time:02-01

I need to add items to a Appointment item.

Appointment is a class as follows:

Appointment Appointment({   
String? startTimeZone,   
String? endTimeZone,   
String? recurrenceRule,   
bool isAllDay = false,   
String? notes,   
String? location,   
List<Object>? resourceIds,   
Object? recurrenceId,   
Object? id,   
required DateTime startTime,   
required DateTime endTime,   
String subject = '',   
Color color = Colors.lightBlue,   
List<DateTime>? recurrenceExceptionDates, })

I am inserting Appointment items as follows:

List<Appointment> appointments = <Appointment>[];
    appointments.add(Appointment(
      startTime: DateTime.now(),
      endTime: DateTime.now().add(const Duration(hours: 1)),
      subject: 'Formación presencial',
      color: Colors.green,
      notes: "Formación presencial",
      location: "Zaragoza",
      resourceIds:  EventObjects(profId: "1",profAvatar: "as",profColor: "dd",profNombre: "ee",eventoColor: "Cc",eventoTipo: "Rr"),

    ),);

But I don´t understand the concept of List of objects, I would like to add as resourcesIds an object from type EventObjects.

class EventObjects{
String? profId;
String? profColor;
String? profNombre;
String? profAvatar;
String? eventoTipo;
String? eventoColor;

// added '?'

  EventObjects({this.profId, this.profColor, this.profAvatar, this.profNombre, this.eventoColor, this.eventoTipo});
// can also add 'required' keyword
}

But I guess this is not the way to do it as the line

resourceIds:  EventObjects(profId: "1",profAvatar: "as",profColor: "dd",profNombre: "ee",eventoColor: "Cc",eventoTipo: "Rr"),

is marked as error

The argument type 'EventObjects' can't be assigned to the parameter type 'List<Object>?'

CodePudding user response:

You are trying to assign here

resourceIds:  EventObjects(profId: "1",profAvatar: "as",profColor: "dd",profNombre: "ee",eventoColor: "Cc",eventoTipo: "Rr"),

a EventObjects instance to a field List<Object>?

What you may want to do is to either declare the resourceIds as Object or EventObjects or make a list literal of resourceIds like this

resourceIds:  [EventObjects(profId: "1",profAvatar: "as",profColor: "dd",profNombre: "ee",eventoColor: "Cc",eventoTipo: "Rr")],

Both will work

CodePudding user response:

Here's the mistake:

Appointment Appointment({   
String? startTimeZone,   
String? endTimeZone,   
String? recurrenceRule,   
bool isAllDay = false,   
String? notes,   
String? location,   
List<Object>? resourceIds,  //here we're assigning as List<Object> which is 
                             // incorrect 
Object? recurrenceId,   
Object? id,   
required DateTime startTime,   
required DateTime endTime,   
String subject = '',   
Color color = Colors.lightBlue,   
List<DateTime>? recurrenceExceptionDates, })

You can do it this way:

Appointment Appointment({   
String? startTimeZone,   
String? endTimeZone,   
String? recurrenceRule,   
bool isAllDay = false,   
String? notes,   
String? location,   
List<EventObjects>? resourceIds, 
Object? recurrenceId,   
Object? id,   
required DateTime startTime,   
required DateTime endTime,   
String subject = '',   
Color color = Colors.lightBlue,   
List<DateTime>? recurrenceExceptionDates, })

And you can assign like this:

resourceIds:[EventObjects(profId: "1",profAvatar: "as",profColor: "dd",profNombre: "ee",eventoColor: "Cc",eventoTipo: "Rr")],

CodePudding user response:

Actually, you are inserting a Object not List<Object> and your datatype for resourceIds is of List<Object>?.

You can try using this:

List<EventObjects>? eventsObjectList;
EventObjects eventObject = EventObjects(profId: "1",profAvatar: "as",profColor: "dd",profNombre: "ee",eventoColor: "Cc",eventoTipo: "Rr"); //Instance of EventObjects
List<Appointment> appointments = <Appointment>[];
eventsObjectList.add(eventObject);
appointments.add(Appointment(
  startTime: DateTime.now(),
  endTime: DateTime.now().add(const Duration(hours: 1)),
  subject: 'Formación presencial',
  color: Colors.green,
  notes: "Formación presencial",
  location: "Zaragoza",
  resourceIds: eventsObjectList),);
  • Related