DART lang, FUTURE function, chain of then

2019-09-10 03:36发布

问题:

I'm using dartOracle, and wanted to use many SQL statements together, but need to make sure the INSERT statement is executed only after getting the table created, I wrote the below code after reading this and this and this and this but it is not working.. any thought!

var resultset;

Future buildDB() {
     var completer = new Completer(); 
     print("Hello, from Future!");
     return completer.future; 
}  

void createTables() {
   Future result= buildDB();

    connect(
       "SYSTEM",
       "password",
       "(DESCRIPTION="
       "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))"
       "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))")
  .then(  
  (oracleConnection) {

  result
        .then((_) => resultset = oracleConnection.select('''
                        CREATE TABLE vendors (
                                     vendor_id NUMBER,
                                     vCode NUMBER,
                                     vName VARCHAR(255),
                                     vEmail VARCHAR(255),
                                     PRIMARY KEY (vendor_id))
                      '''))   
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (1,'code1','name1','email1')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (2,'code2','name2','email2')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (3,'code3','name3','email3')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => print('tables created!'));  
  }, 
  onError: (error) {
    print("Failed to create tables, error found: $error");
  });   
}

once I execute the function, I get this:

Observatory listening on http://127.0.0.1:54590
 Hello, vendor!
 Hello, from Future!
 Listening for GET and POST on http://127.0.0.1:8004

and nothing happen after that, I waited for 5 minutes, with no changes!

回答1:

You never call completer.complete(). So your result Future will never get any data and thus the .then()-chain will never execute.



回答2:

thanks everyone, the below code worked perfectly with me:

pubspec.yaml:

dependencies:
  oracledart: any

the main.dart file:

import 'dart:async';
import 'package:oracledart/oracledart.dart';

void main() {
   Future buildDB() => new Future.value(true);     // dummy Future function to ensure SQL statements done in the proper sequence
   print("Hello to vendor tables setup!");
   var vendors = <Map>[];

   connect(
    "SYSTEM",
    "password",
    "(DESCRIPTION="
      "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))"
      "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))")
.then(  
  (oracleConnection) {

  buildDB()
        .then((_) => print('Pls wait, sequence of SQL statements will be executed now'))
        .then((_) => oracleConnection.select("""
                    CREATE TABLE vendors (
                                 vendor_id NUMBER,
                                 vCode NUMBER,
                                 vName VARCHAR(255),
                                 vEmail VARCHAR(255),
                                 PRIMARY KEY (vendor_id))
                     """))
        .then((_) => print('table had been created, now will start inserting initial availabe data!'))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (1,101,'vName1','vEmail1@email.com')"))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (2,102,'vName2','vEmail2@email.com')"))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (3,103,'vName3','vEmail3@email.com')"))
        .then((_) => print('data had been inserted, now will run a SELECT statement to show you what had been inserted!'))
        .then((_) {
            var resultset = oracleConnection.select("select * from vendors");
            while(resultset.next()) {
                 print("hello this is: ${resultset.getStringByName('VNAME')}");
                  vendors.add({"code":"${resultset.getStringByName('VCODE')}",
                    "name": "${resultset.getStringByName('VNAME')}",
                    "email": "${resultset.getStringByName('VEMAIL')}"
                  });
               }              
                print('the data entered is:  $vendors'); 
              })
        .then((_) => print('Done, SQL statement completed!'));
  }, 
  onError: (error) {
    print("Failed to connect: $error");
  });   
}


标签: dart future