0

I am trying to set the values of a custom document record object in Flutterflow. I created a collection and a Location document type. I want to pass a list of these Location Documents to the GoogleMaps component of Flutterflow and it only allows to attach data for markers when you use documents. So I can't use a list custom data types for it.

I had this code already working with the custom data type, but now I need to rewrite it to make it work with the document records.

How to set values(name, description, latLng) to the LocationRecord object? I don't need to save them to a database. I just need these objects to trick the Google Map component, since my location data is coming from an API.

  List<LocationRecord> createLocationRecordsFromJson(dynamic jsonMarkers) {
  /// MODIFY CODE ONLY BELOW THIS LINE

  List<LocationRecord> locations = [];
  dynamic markers = jsonMarkers['markers'];
  markers.forEach((element) {
    LocationRecord location = new LocationRecord();
    /// something like location.set('name', element['title']);
    locations.add(location);
  });
  return locations;

  /// MODIFY CODE ONLY ABOVE THIS LINE
}
Abenil
  • 1,048
  • 3
  • 12
  • 26

2 Answers2

1
class LocationRecord {
  String name;
  String description;
  dynamic latLng; //  adjust the data type

  LocationRecord({
    required this.name,
    required this.description,
    required this.latLng,
  });
}

List<LocationRecord> createLocationRecordsFromJson(dynamic jsonMarkers) {
  List<LocationRecord> locations = [];
  dynamic markers = jsonMarkers['markers'];
  
  markers.forEach((element) {
    // Assuming 'title' and other relevant fields are in element
    String name = element['title'];
    String description = element['description'];
    dynamic latLng = element['latLng']; // Adjust the key 

    LocationRecord location = LocationRecord(
      name: name,
      description: description,
      latLng: latLng,
    );
    
    locations.add(location);
  });

  return locations;
}

make sure you have access to the necessary fields from the element in your JSON data. You mentioned 'title' as one of the fields and btw also make sure 'description' and 'latLng'are available in your API response.

  • Thanks for your reply, but I am not sure how this is supposed to work. You defined a LocationRecord class that is not extending the original LocationRecord or the FirestoreRecord. The LocationRecord is generated by Flutterflow from how I understand it. – Abenil Aug 27 '23 at 13:24
  • I tried it and it's not working for the reasons I mentioned before. LocationRecord is a class provided by Flutterflow and not something custom. – Abenil Aug 27 '23 at 13:33
  • Is it not working or is it showing some kind of error dialogs? – Dinesh Kumar M Aug 27 '23 at 16:29
  • I cant even paste in the LocationRecord class, because it's only allowed to modify the code in the function body in Flutterflow. – Abenil Aug 27 '23 at 17:21
  • And when I create a custom action to create that class and return then exactly that happens, what I expected to happen. It complains that the return types don't match. My custom function is now returning a LocationRecord defined in lib/custom_code/actions/new_custom_action.dart instead of lib/backend/schema/location_record.dart and it doesn't compile for that reason. – Abenil Aug 27 '23 at 17:49
0

It looks like you've created a custom datatype so you should be able to follow this guideline: https://docs.flutterflow.io/data-and-backend/custom-data-types/custom-data-type-in-custom-code

List<LocationRecord> createLocationRecordsFromJson(dynamic jsonMarkers) {
  final markers = jsonMarkers['markers'] as Map?;
  return markers?.map((m) => LocationRecord.fromMap(m)).toList() ?? [];
}
d-markey
  • 163
  • 1
  • 7