| name | google-cloud-compute-v1-setup |
| description | Use this skill to help the developer get started with package:google_cloud_compute_v1 |
Getting Started with package:google_cloud_compute_v1
1. Add the package to your pubspec.yaml
dart pub add google_cloud_compute_v1
2. Import the library
In your Dart code, import the library:
import 'package:google_cloud_compute_v1/compute.dart';
3. Initialize the client
To call the client, you need to create an instance of the service that
you want to use, e.g., FirewallPolicies.
There are two ways of creating an instance with the necessary credentials.
Option A: Using Application Default Credentials (ADC) (Recommended)
Recommended for production environments, server-side backends, and when
running on Google Cloud (such as Cloud Run, GCE, GKE).
When running on Google Cloud, Application Default Credentials are automatically
available.
When running locally, the gcloud command must be available (see
Install the Google Cloud CLI).
To obtain Application Default Credentials, run:
gcloud auth application-default login
Add googleapis_auth to your dependencies:
dart pub add googleapis_auth
import 'package:googleapis_auth/auth_io.dart';
import 'package:google_cloud_compute_v1/compute.dart';
Future<void> main() async {
final authClient = await clientViaApplicationDefaultCredentials(
scopes: [
'https://www.googleapis.com/auth/cloud-platform',
],
);
final client = FirewallPolicies(client: authClient);
try {
// Call a method on the client.
final response = await client.listAssociations(
ListAssociationsFirewallPolicyRequest(),
);
print('Response: $response');
} finally {
// Always close the client to release resources.
client.close();
}
}
Option B: Using an API Key (Simple)
Recommended for quick testing or prototyping because the setup is simple.
If the user does not have an API key, then they can obtain one using the
Google Cloud Console.
Add the API key to one of these environment variables:
import 'package:google_cloud_compute_v1/compute.dart';
Future<void> main() async {
final client = FirewallPolicies.fromApiKey();
try {
// Call a method on the client.
final response = await client.listAssociations(
ListAssociationsFirewallPolicyRequest(),
);
print('Response: $response');
} finally {
// Always close the client to release resources.
client.close();
}
}