Connect Flutter to Odoo

Kururu
3 min readOct 17, 2022

--

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

the method used to connect to odoo server called RPC (remote procedure call ).

accroding to wikipedia :

a remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to execute in a different address space (commonly on another computer on a shared network), which is coded as if it were a normal (local) procedure call, without the programmer explicitly coding the details for the remote interaction. That is, the programmer writes essentially the same code whether the subroutine is local to the executing program, or remote. This is a form of client–server interaction (caller is client, executor is server), typically implemented via a request–response message-passing system. In the object-oriented programming paradigm, RPCs are represented by remote method invocation (RMI). The RPC model implies a level of location transparency, namely that calling procedures are largely the same whether they are local or remote, but usually, they are not identical, so local calls can be distinguished from remote calls. Remote calls are usually orders of magnitude slower and less reliable than local calls, so distinguishing them is important.

in flutter there is a package to make RPC called odoo_rpc.

first of all add the package to your pubspec.yaml file:

dependencies:
flutter:
sdk: flutter
flutter_localizations: # Add this line
sdk: flutter # Add this line
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2

odoo_rpc: ^0.5.1

lets see how the very basic example look like:

import ‘package:flutter/material.dart’;
import ‘package:odoo_rpc/odoo_rpc.dart’;
final orpc = OdooClient(‘https://my-odoo-instance.com');
void main() async {
await orpc.authenticate(‘odoo-db’, ‘admin’, ‘admin’);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
Future<dynamic> fetchContacts() {
return orpc.callKw({
‘model’: ‘res.partner’,
‘method’: ‘search_read’,
‘args’: [],
‘kwargs’: {
‘context’: {‘bin_size’: true},
‘domain’: [],
‘fields’: [‘id’, ‘name’, ‘email’, ‘__last_update’, ‘image_128’],
‘limit’: 80,
},
});
}
Widget buildListItem(Map<String, dynamic> record) {
var unique = record[‘__last_update’] as String;
unique = unique.replaceAll(RegExp(r’[⁰-9]’), ‘’);
final avatarUrl =
‘${orpc.baseURL}/web/image?model=res.partner&field=image_128&id=${record[“id”]}&unique=$unique’;
return ListTile(
leading: CircleAvatar(backgroundImage: NetworkImage(avatarUrl)),
title: Text(record[‘name’]),
subtitle: Text(record[‘email’] is String ? record[‘email’] : ‘’),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘Contacts’),
),
body: Center(
child: FutureBuilder(
future: fetchContacts(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
final record =
snapshot.data[index] as Map<String, dynamic>;
return buildListItem(record);
});
} else {
if (snapshot.hasError) return Text(‘Unable to fetch data’);
return CircularProgressIndicator();
}
}),
),
);
}
}

the important classes in this package is :

OdooSession:

session that use to authenticate your connection

if expired you have to log again

OdooClient:

to connect to odoo server with DB , user name, and passwrod.

this will give a session id so you can make requests .

OdooException:

handle odoo server exceptions.

the request body look like this:

orpc.callKw({
‘model’: ‘res.partner’,
‘method’: ‘search_read’,
‘args’: [],
‘kwargs’: {
‘context’: {‘bin_size’: true},
‘domain’: [],
‘fields’: [‘id’, ‘name’, ‘email’, ‘__last_update’, ‘image_128’],
‘limit’: 80,
},
});

oprc is the OdooClient Object

model : odoo model , database has many models

method: the requst method ,search_read is just to get data , create to create new record in the model.

kwargs ->domain here is the query filter

‘domain’: [
[“product_template_id”, “=”, id]
]

fields: specify which fields you need to retrieve

that’s it:)

i hope you enjoy reading

thanks

--

--

Kururu
Kururu

Written by Kururu

Android | Flutter | Data Analytics

No responses yet