vendure 0.6.0 vendure: ^0.6.0 copied to clipboard
Flutter SDK for Vendure Open Source Headless Commerce Framework.
Vendure Flutter SDK #
A Flutter SDK for interacting with the Vendure e-commerce framework's GraphQL API. This SDK simplifies the process of connecting to Vendure and performing common operations like authentication, adding items to the cart, and more.
Features #
- Authenticate users
- Add items to the cart
- Extendable to support custom GraphQL operations
Installation #
Add the following to your pubspec.yaml
:
dependencies:
vendure_sdk: ^0.1.0
Usage #
First, create an instance of the Vendure class:
import 'package:vendure_sdk/vendure.dart';
void main() async {
//initialize with Native Auth
Vendure vendure = await Vendure.initializeWithNativeAuth(
endpoint: 'http://localhost:3000/shop-api',
username: 'your-username',
password: 'your-password',
sessionDuration: const Duration(days: 1), // Example session duration
);
//initialize with Firebase Auth
vendure = await Vendure.initializeWithFirebaseAuth(
endpoint: 'http://localhost:3000/shop-api',
uid: '8o6CuL3vvceCwjnSxtCTp08vEMr2',
jwt: 'your-jwt-token',
sessionDuration: const Duration(hours: 1), // Example session duration
);
//initialize with Token
vendure = await Vendure.initialize(
endpoint: 'http://localhost:3000/shop-api',
token:
'9a3d1222ed018701fdd8a7484a7299977507787f5bb22bec898e67939ee453169f8');
//initialize with Custom Auth
vendure = await Vendure.initializeWithCustomAuth(
endpoint: 'http://localhost:3000/shop-api',
fetchToken: (params) async {
// Implement your custom token fetching logic here
return 'custom-token';
},
tokenParams: {
'customParam1': 'value1',
'customParam2': 'value2',
},
sessionDuration: const Duration(days: 1), // Example session duration
);
// Use the vendure instance for various operations
Vendure vendure = Vendure.instance;
}
Firebase Authentication #
Firebase updates tokens every hour. You should listen and update Vendure client with new token
Future<void> initializaRepository() async {
FirebaseAuth.instance.idTokenChanges().listen((event) async {
if (event == null) return;
String? idToken = await event.getIdToken();
if (idToken == null) {
throw Exception('idToken is null');
}
await Vendure.initializeWithFirebaseAuth(
endpoint: 'http://localhost:3000/shop-api', uid: event.uid, jwt: idToken);
});
}
Order #
Future<UpdateOrderItemsResult> addItemToOrder(int productVariantId, int quantity) async {
return _vendure.order.addItemToOrder(
productVariantId: productVariantId,
quantity: quantity,
);
}
Future<ActiveOrderResult> setOrderShippingAddress(
CreateAddressInput input) async {
return _vendure.order.setOrderShippingAddress(input);
}
Custom Operations #
You can also perform custom operations using the custom
method:
Just pass in the expected data type and your response handler ie CustomMutationResult.fromJson
Future<void> customMutation(Vendure vendure) async {
try {
const String myCustomMutation = r'''
type CustomMutationInput{
customField: String!
}
mutation CustomMutation($input:CustomMutationInput) {
customMethod(input:$input){
__typename
{
customField
}
}
}
''';
var variables = {
"customField": 'your-custom-value'
};
final result = await vendure.custom.mutate(
myCustomMutation, variables, CustomMutationResult.fromJson,
expectedDataType: 'custom');
print('Custom result: ${result.customField}');
} catch (e) {
print('Error custom mutation : $e');
}
}
//same as above pass your result handlerMethod ie CustomQueryResult.fromJson
Future<void> customQuery(Vendure vendure) async {
try {
const String myCustomQuery = r'''
type CustomQueryInput{
customField: String!
}
mutation CustomQuery($input:CustomQueryInput) {
customMethod(input:$input){
__typename
{
customField
}
}
}
''';
var variables = {
"customField": 'your-custom-value'
};
final result = await vendure.custom.query(
myCustomQuery, variables, CustomQueryResult.fromJson,
expectedDataType: 'custom');
print('Custom result: ${result.customField}');
} catch (e) {
print('Error custom mutation : $e');
}
}
Future<void> customQuery(Vendure vendure) async {
try {
final result = await vendure.custom.query(
customQuery, variables, CustomResult.fromJson,
expectedDataType: 'custom');
print('Custom result: ${result.customField}');
} catch (e) {
print('Error custom query : $e');
}
}
Contributing #
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.