Select to view content in your preferred language

ArcGISCredential.fromJson() throws exception "Null check operator used on a null value"

75
1
Tuesday
Labels (2)
AM01
by
New Contributor

Hi All,

I am trying to add the credentials via ArcGISEnvironment.authenticationManager.arcGISCredentialStore.addForUri()

I have logged into the AGOL via WebView and after successful login. I am trying to add the credentials. I tried following,

      final credentialMap = {
        "token": token,
        "userId": username,
        "expiration": expireTime.toIso8601String(),
      };

      final tokenCredential = ArcGISCredential.fromJson(credentialMap);

      // Add credential to the store
     ArcGISEnvironment.authenticationManager.arcGISCredentialStore.addForUri(
        credential: tokenCredential,
        uri: Uri.parse(portalURL),
      );

      final portal = Portal.arcGISOnline(
        connection: PortalConnection.authenticated,
      );
      await portal.load();

 
I checked every thing none of value is null in credentialMap, but I am still getting an exception line no. 7 - "Null check operator used on a null value".
Not sure what I am missing here. Any help would be appreciated.

fyi, I successfully login and have the token and other user details, its just I want to use Portal APIs exposed by SDK. I am looking to get the license info so that app could work offline and use portal APIs. It appears there is no url to get the license via http call.
0 Kudos
1 Reply
kossiyovo
Esri Contributor

You don't need to use WebView to login. You can do that directly through the Flutter Maps SDK using TokenCredential with this approach:

final credential = await TokenCredential.create(
  uri: Uri.parse(portalURL),
  username: username,
  password: password,
);

Add the credential to the credential store: This is where you were getting the null error. Use the TokenCredential directly:

ArcGISEnvironment.authenticationManager.arcGISCredentialStore.addForUri(
  credential: credential,  // Use the TokenCredential, not a JSON map
  uri: Uri.parse(portalURL),
);

Create and load the authenticated portal:

final portal = Portal.arcGISOnline(
  connection: PortalConnection.authenticated,
);
await portal.load();

Get the license info for offline functionality: This is what you need for offline Portal APIs:

final licenseInfo = await portal.fetchLicenseInfo();
print('License: ${licenseInfo.toJson()}');


// You'd get something like: {licenseString: LICENSE_KEY}

This should eliminate your null pointer exception and get you the offline licensing you're looking for.

Checkout this sample for a full example: https://github.com/Esri/arcgis-maps-sdk-flutter-samples/tree/main/lib/samples/authenticate_with_toke...

Let me know if you have any questions!

Product Engineer II at Esri, Google Developer Expert for Flutter/Dart
0 Kudos