To use the SDK in a Dart project, first install the SDK:
dart pub add cloudinary_api
dart pub add cloudinary_url_genThen, add the necessary package references
import 'dart:io';
import 'package:cloudinary_url_gen/cloudinary.dart';
import 'package:cloudinary_api/uploader/cloudinary_uploader.dart';Finally, configure a new instance of Cloudinary:
final cloudName = 'CLOUDINARY_CLOUD_NAME';
final apiKey = 'CLOUDINARY_API_KEY';
final apiSecret = 'CLOUDINARY_API_SECRET';
final cloudinary = Cloudinary.fromStringUrl('cloudinary://${apiKey}:${apiSecret}@${cloudName}');Note: always use environment variables in production environments
Finally, run the upload method, passing in your image source:
var uploadResult = await cloudinary.uploader().upload(File('localImagePath'));
// OR
var uploadResult = await cloudinary.uploader().upload('onlineImageURL');- Rename the file
.env.exampleto.envand update the following environment variables
CLOUDINARY_CLOUD_NAME=<Your Cloud Name>
CLOUDINARY_API_KEY=<Your API Key>
CLOUDINARY_API_SECRET=<Your API Secret>
- Install the project dependencies with:
dart pub get- Run the program with:
dart run bin/dart_image_upload.dartThe program will run in your terminal and you'll see the URL results logged.