Stellar SDK for Flutter Quick Start
// create a completely new and unique pair of keys.
KeyPair keyPair = KeyPair.random();
print("${keyPair.accountId}");
// GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB
print("${keyPair.secretSeed}");
// SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7
After the key pair generation, you have already got the address, but it is not activated until someone transfers at least 1 lumen into it.
If you want to play in the Stellar test network, the SDK can ask Friendbot to create an account for you as shown below:
bool funded = await FriendBot.fundTestAccount(keyPair.accountId);
print ("funded: ${funded}");
On the other hand, if you would like to create an account in the public net, you should buy some Stellar Lumens (XLM) from an exchange. When you withdraw the Lumens into your new account, the exchange will automatically create the account for you. However, if you want to create an account from another account of your own, you may run the following code:
/// Create a key pair for your existing account.
KeyPair keyA = KeyPair.fromSecretSeed("SAPS66IJDXUSFDSDKIHR4LN6YPXIGCM5FBZ7GE66FDKFJRYJGFW7ZHYF");
/// Load the data of your account from the stellar network.
AccountResponse accA = await sdk.accounts.account(keyA.accountId);
/// Create a keypair for a new account.
KeyPair keyB = KeyPair.random();
/// Create the operation builder.
CreateAccountOperationBuilder createAccBuilder = CreateAccountOperationBuilder(keyB.accountId, "3"); // send 3 XLM (lumen)
// Create the transaction.
Transaction transaction = new TransactionBuilder(accA, Network.PUBLIC)
.addOperation(createAccBuilder.build())
.build();
/// Sign the transaction with the key pair of your existing account.
transaction.sign(keyA);
/// Submit the transaction to the stellar network.
SubmitTransactionResponse response = await sdk.submitTransaction(transaction);
if (response.success) {
print ("account ${keyB.accountId} created");
}
After creating the account, we may check the basic information of the account.
String accountId = "GASYKQXV47TPTB6HKXWZNB6IRVPMTQ6M6B27IM5L2LYMNYBX2O53YJAL";
// Request the account data.
AccountResponse account = await sdk.accounts.account(accountId);
// You can check the `balance`, `sequence`, `flags`, `signers`, `data` etc.
for (Balance balance in account.balances) {
switch (balance.assetType) {
case Asset.TYPE_NATIVE:
print("Balance: ${balance.balance} XLM");
break;
default:
print("Balance: ${balance.balance} ${balance
.assetCode} Issuer: ${balance.assetIssuer}");
}
}
print("Sequence number: ${account.sequenceNumber}");
for (Signer signer in account.signers) {
print("Signer public key: ${signer.accountId}");
}
for (String key in account.data.keys) {
print("Data key: ${key} value: ${account.data[key]}");
}
You can check the payments connected to an account:
Page<OperationResponse> payments = await sdk.payments.forAccount(accountAId).order(RequestBuilderOrder.DESC).execute();
for (OperationResponse response in payments.records) {
if (response is PaymentOperationResponse) {
PaymentOperationResponse por = response as PaymentOperationResponse;
if (por.transactionSuccessful) {
print("Transaction hash: ${por.transactionHash}");
}
}
}
You can use:limit
, order
, and cursor
to customize the query. Get the most recent payments for accounts, ledgers and transactions.
Just like payments, you you check assets
, transactions
, effects
, offers
, operations
, ledgers
etc.
sdk.assets.
sdk.transactions.
sdk.effects.
sdk.offers.
sdk.operations.
sdk.orderBook.
sdk.trades.
// add so on ...
Example "send native payment":
KeyPair senderKeyPair = KeyPair.fromSecretSeed("SAPS66IJDXUSFDSDKIHR4LN6YPXIGCM5FBZ7GE66FDKFJRYJGFW7ZHYF");
String destination = "GDXPJR65A6EXW7ZIWWIQPO6RKTPG3T2VWFBS3EAHJZNFW6ZXG3VWTTSK";
// Load sender account data from the stellar network.
AccountResponse sender = await sdk.accounts.account(senderKeyPair.accountId);
// Build the transaction to send 100 XLM native payment from sender to destination
Transaction transaction = new TransactionBuilder(sender, Network.TESTNET)
.addOperation(PaymentOperationBuilder(destination,Asset.NATIVE, "100").build())
.build();
// Sign the transaction with the sender's key pair.
transaction.sign(senderKeyPair);
// Submit the transaction to the stellar network.
SubmitTransactionResponse response = await sdk.submitTransaction(transaction);
if (response.success) {
print("Payment sent");
}
To continue, please have a look to our examples here.