Note: This guide is for developers aiming to integrate the Haveno client SDK in Flutter applications, using the Dart package from pub.dev.
This guide will walk you through setting up a development environment, installing the Haveno Dart SDK, and configuring the SDK to interact with the Haveno client. By the end, you'll have a fully functioning setup to start building Haveno-integrated Flutter applications.
Ensure the following prerequisites are met before starting with the setup:
Tip: Make sure you have Flutter correctly set up by running
flutter doctor
in the terminal to identify and resolve any environment issues.
Create a New Flutter Project: Open your terminal and run the following command:
flutter create haveno_flutter_bot
Navigate to the Project Directory:
cd haveno_flutter_bot
Open the Project: Open the project in your preferred IDE.
Add the Haveno Dart SDK as a dependency in your pubspec.yaml
file to enable Haveno functionality within your app.
Add the Dependency: Open pubspec.yaml
and add the following dependency under dependencies
:
dependencies:
flutter:
sdk: flutter
haveno: latest_version # Replace 'latest_version' with the actual latest version from pub.dev
Install the Package: Run the following command in the terminal to install the Haveno package:
flutter pub get
Success: The Haveno package should now be installed and available in your project.
The Haveno SDK requires certain configurations to connect to the Haveno client.
To securely store API keys or configurations, you may want to use environment variables. Set up an .env
file in the root of your project for easy configuration.
Install the flutter_dotenv
package (optional):
Add to pubspec.yaml
:
dependencies:
flutter_dotenv: latest_version
Add the .env
File: Create a .env
file in the root of your project and add your Haveno client settings:
HAVENO_HOST=http://localhost
HAVENO_PORT=8080
Load Environment Variables (optional):
Add this line in your main Dart file to load .env
variables:
import 'package:flutter_dotenv/flutter_dotenv.dart';
void main() async {
await dotenv.load();
runApp(MyApp());
}
The Haveno client can now be initialized and configured for interaction.
Import the Haveno Package:
import 'package:haveno/haveno.dart';
Initialize the Client:
Use environment variables or direct configuration to initialize the client.
final havenoClient = HavenoClient(
host: dotenv.env['HAVENO_HOST'] ?? 'http://localhost',
port: int.parse(dotenv.env['HAVENO_PORT'] ?? '8080'),
);
Connecting to the Haveno Node:
Call connect()
to initialize the connection with the Haveno client.
void connectToHaveno() async {
await havenoClient.connect();
print('Connected to Haveno');
}
Use the following method to check the connection status with the Haveno client:
Future<void> checkConnectionStatus() async {
bool isConnected = await havenoClient.isConnected();
print('Connection Status: $isConnected');
}
Future<void> getAccountBalance() async {
final balance = await havenoClient.getBalance();
print('Account Balance: ${balance.total}');
}
Future<void> sendTransaction(String recipientAddress, double amount) async {
await havenoClient.sendTransaction(
address: recipientAddress,
amount: amount,
);
print('Transaction sent to $recipientAddress with amount $amount');
}
Always handle errors to ensure the stability of your app. Here's an example of error handling when calling Haveno functions.
try {
await havenoClient.connect();
print('Connected to Haveno');
} catch (e) {
print('Failed to connect: $e');
}
Warning: Ensure proper error handling, as network errors and API call issues can disrupt app functionality.
Run the Flutter App: Test the setup by running the app in debug mode:
flutter run
Verify Connections: Ensure that the app can connect to the Haveno client and perform actions such as retrieving balance or sending transactions.
Info: The Haveno Dart SDK provides many other functions for managing accounts, monitoring transactions, and interacting with the Haveno network. Check the Haveno Dart SDK documentation for a full list of supported functions.
By following this guide, you should have a fully configured Flutter environment ready to interact with Haveno using the Dart SDK. This setup provides a foundation to start building Haveno-integrated applications on Flutter. Happy coding!