orderHistory Method
This method is used to get the details of all the past orders of the current user.
Parameters
orderId
- The order id to get the specific order details.offset
- The number of items to skip before starting to collect the result set. Default is 0.limit
- The number of items to return. Default is 1.orderState
- The order state to filter the orders. Possible values arePENDING
,SUCCESS
,FAILED
.
await okto.orderHistory(
orderState: OrderState.success,
orderId: 'YOUR_ORDER_ID',
limit: 1,
offset: 0
);
Example
import 'package:my_app/okto.dart';
import 'package:flutter/material.dart';
import 'package:okto_flutter_sdk/okto_flutter_sdk.dart';
class OrderHistoryPage extends StatefulWidget {
const OrderHistoryPage({super.key});
@override
State<OrderHistoryPage> createState() => _OrderHistoryPageState();
}
class _OrderHistoryPageState extends State<OrderHistoryPage> {
Future<OrderHistoryResponse>? _orderHistory;
Future<OrderHistoryResponse> getOrderHistory() async {
try {
final orderHistory = await okto!.orderHistory();
return orderHistory;
} catch (e) {
throw Exception(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xff5166EE),
body: SafeArea(
child: Column(
children: [
Container(
alignment: Alignment.center,
margin: const EdgeInsets.all(40),
child: const Text(
'Order History',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.w800, fontSize: 30),
),
),
ElevatedButton(
onPressed: () {
setState(() {
_orderHistory = getOrderHistory();
});
},
child: const Text('Order History'),
),
Expanded(
child: _orderHistory == null
? Container()
: FutureBuilder<OrderHistoryResponse>(
future: _orderHistory,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator(color: Colors.white));
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (snapshot.hasData) {
final orderHistory = snapshot.data!;
return Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Status: ${orderHistory.status}'),
Text('Total: ${orderHistory.data.total}'),
SizedBox(
height: MediaQuery.sizeOf(context).height * 0.6,
child: ListView.builder(
itemCount: orderHistory.data.jobs.length,
itemBuilder: (context, index) {
return Container(
color: Colors.blue,
margin: const EdgeInsets.all(5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Network Name: ${orderHistory.data.jobs[index].networkName}',
style: const TextStyle(color: Colors.white, fontSize: 20),
),
Text(
'Order Id : ${orderHistory.data.jobs[index].orderId}',
style: const TextStyle(color: Colors.white, fontSize: 20),
),
Text(
'Order Type : ${orderHistory.data.jobs[index].orderType}',
style: const TextStyle(color: Colors.white, fontSize: 20),
),
Text(
'Status : ${orderHistory.data.jobs[index].status}',
style: const TextStyle(color: Colors.white, fontSize: 20),
),
Text(
'Transaction Hash: ${orderHistory.data.jobs[index].transactionHash}',
style: const TextStyle(color: Colors.white, fontSize: 20),
),
],
),
);
}),
)
// Add more fields here as needed
],
),
);
}
return Container();
},
),
),
],
),
),
);
}
}