Esri / arcgis-maps-sdk-flutter-samples

Sample code for ArcGIS Maps SDK for Flutter
Apache License 2.0
10 stars 5 forks source link

How to set Api Key for private Feature Layer #80

Closed nhimrmh closed 1 month ago

nhimrmh commented 1 month ago

Hello, Currently I run into an issue when trying to add a private Feature Layer to Gis map, error:

An ArcGIS authentication error occurred when accessing "[feature_layer_url]". An API key or credential may be required. For more information see (https://developers.arcgis.com/documentation/mapping-apis-and-services/security/).

Since there's no information or any example that mentions about setting api key to feature layer, I wonder how that can be done?

I've tried this but not work

ArcGISEnvironment.apiKey = [apiKey]; // <- Already set api key for ArcGIS environment here

// Define service feature table
final serviceFeatureTable = ServiceFeatureTable.fromUri(
   Uri.parse(
      '[feature_layer_url]',
   ),
);

// Define feature layer with service feature table above
final featureLayer = FeatureLayer.withFeatureTable(
   serviceFeatureTable..apiKey = widget.apiKey,
);

// Add feature layer to current map
map.operationalLayers.add(featureLayer); // <- Get error for API key here
mapViewController.arcGISMap = map;
jenmerritt commented 1 month ago

Hi there! Thanks for reaching out. You are correct to set your API key via ArcGISEnvironment.apiKey. You don't need to set it specifically on, e.g. the feature layer. So that part is good.

An initial thing to check - are you calling ArcGISEnvironment.apiKey = [apiKey]; in the main() method before you call runApp?

i.e. in your main.dart file:

void main() {
  ArcGISEnvironment.apiKey = [apiKey];
  runApp(MyApp());
}
jenmerritt commented 1 month ago

Closing as resolved. If you have further questions regarding setting the API key, feel free to reopen this issue.