Use same connection to odoo instance with odoo_rpc

719 views Asked by At

Three days ago i asked same question but I got no answer, So here I am again asking. is there a way to keep the same connection (client) after login to odoo instance and work with it across pages, if so how it's done ?

odoo rpc_package link

import 'package:odoo_rpc/odoo_rpc.dart';
Future<bool> _loggin(email, password) async {
    final client = OdooClient('http://localhost:8069');
    // String database = 'CENTRAL-KAD';
    String database = 'odoo';
    String email = 'admin';
    String password = "admin";
    
    if (_formKey.currentState!.validate()) {
      try {
        final session = await client.authenticate(database, email, password);
        final res = await client.callRPC('/web/session/modules', 'call', {});
        print('Installed modules: \n' + res.toString());
        
        Navigator.push(context,
         MaterialPageRoute(builder: (context) => const welcomscreen()),
        );

        } on OdooException catch (e) {
          print(e);

          
        }
    }
}

Welcome_screen

I want to get the connection which is client in this page so to CRUD with it, and without openning new connection

class welcomeScreen extends StatefulWidget {
  welcomeScreen();
  @override
  _welcomeScreenState createState() => _welcomeScreenState();
}

class _welcomeScreenState extends State<welcomeScreen> {

  _welcomeScreenState();

  @override
  Widget build(BuildContext context) {
    return Scaffold( 
        ///... some view ...
        child: MaterialButton(
            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
            shape: const StadiumBorder(),
            child: const Text(
              'Find Customer', 
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 32.0,
                  fontWeight: FontWeight.bold,
                ),
              ),
            onPressed: (){
              client.callKw({
                'model': 'res.partner',
                'method': 'search_read',
                'args': [],
                'kwargs': {
                  'context': {'bin_size': true},
                  'domain': [],
                  'fields': ['id', 'name', 'email', '__last_update', 'image_128'],
                  'limit': 80,
                },
              });
            }
          )
    );

  }
1

There are 1 answers

0
Imad fed On

i found the answer, all you i have to do is pass client while navigating to other page as following:

Navigator.push(context,
     MaterialPageRoute(builder: (context) => const welcomscreen(client)),
    );



class welcomeScreen extends StatefulWidget {

final OdooClient client;

welcomeScreen(this.client);

@override
_welcomeScreenState createState() => _welcomeScreenState();

}

class _welcomeScreenState extends State<welcomeScreen> {
 .
 .
 child: MaterialButton(
   onPressed: () { 
     // use widget.client 
   }
}