我正在使用Flutter创建一个简单的应用程序,并希望连接到mysql数据库。我用作dart代码的驱动程序的插件是here。
但是,我收到以下错误:
[ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 4789): 'package:sqljocky5/src/buffered_socket.dart': error: Not a constant expression: unexpected kernel tag InvalidExpression (19)
E/flutter ( 4789): #0 Connection.connect (package:sqljocky5/src/connection.dart:124:20)
E/flutter ( 4789): <asynchronous suspension>
捕获错误会产生相同的错误:
I/flutter ( 4789): opening connection
I/flutter ( 4789): connection open
I/flutter ( 4789): running example
I/flutter ( 4789): dropping tables
I/flutter ( 4789): 'package:sqljocky5/src/buffered_socket.dart': error: Not a constant expression: unexpected kernel tag InvalidExpression (19)
用于实例化连接的方法是从驱动程序的github示例中复制的:
class Example {
ConnectionPool pool;
Example(this.pool);
Future run() async {
// drop the tables if they already exist
await dropTables();
print("dropped tables");
// then recreate the tables
await createTables();
print("created tables");
// add some data
await addData();
// and read it back out
await readData();
}
Future dropTables() {
print("dropping tables");
var dropper = new TableDropper(pool, ['pets', 'people']);
return dropper.dropTables();
}
Future createTables() {
print("creating tables");
var querier = new QueryRunner(pool, [
'create table people (id integer not null auto_increment, '
'name varchar(255), '
'age integer, '
'primary key (id))',
'create table pets (id integer not null auto_increment, '
'name varchar(255), '
'species text, '
'owner_id integer, '
'primary key (id),'
'foreign key (owner_id) references people (id))'
]);
print("executing queries");
return querier.executeQueries();
}
Future addData() async {
var query =
await pool.prepare("insert into people (name, age) values (?, ?)");
print("prepared query 1");
var parameters = [
["Dave", 15],
["John", 16],
["Mavis", 93]
];
await query.executeMulti(parameters);
print("executed query 1");
query = await pool
.prepare("insert into pets (name, species, owner_id) values (?, ?, ?)");
print("prepared query 2");
parameters = [
["Rover", "Dog", 1],
["Daisy", "Cow", 2],
["Spot", "Dog", 2]
];
// ["Spot", "D\u0000og", 2]];
await query.executeMulti(parameters);
print("executed query 2");
}
Future readData() async {
print("querying");
var result =
await pool.query('select p.id, p.name, p.age, t.name, t.species '
'from people p '
'left join pets t on t.owner_id = p.id');
print("got results");
return result.forEach((row) {
if (row[3] == null) {
print("ID: ${row[0]}, Name: ${row[1]}, Age: ${row[2]}, No Pets");
} else {
print(
"ID: ${row[0]}, Name: ${row[1]}, Age: ${row[2]}, Pet Name: ${row[3]}, Pet Species ${row[4]}");
}
});
}
}
main() async {
// OptionsFile options = new OptionsFile('connection.options');
String user = "root";
String password = "password";
int port = 3306;
String db = "db1";
String host = "localhost";
// create a connection
print("opening connection");
var pool = new ConnectionPool(
host: host, port: port, user: user, password: password, db: db, max: 1);
print("connection open");
// create an example class
var example = new Example(pool);
// run the example
print("running example");
await example.run();
// finally, close the connection
print("K THNX BYE!");
pool.closeConnectionsNow();
}
我怀疑这可能与Dart 2最近包含在Flutter中有什么关系,这打破了这个驱动程序。我想尝试修复这个插件,但我不知道从哪里开始以及错误表示的是什么。