skip to Main Content

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
}

2

Answers


  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.

    Login or Signup to reply.
  2. 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() ?? [];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search