我试图创建一个数据库,并在创建和插入一些数据之后,从一个SELECT获取这些数据。
但在第一次尝试打开应用程序时,我创建了表,之后不会移动到下一个屏幕,我永远留在这个屏幕上:
Infinite load screen on FIRST RUN
this return from initDB methods
我的数据库类:
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
class ConfigDatabase {
static final ConfigDatabase _instance = ConfigDatabase._internal();
factory ConfigDatabase() => _instance;
static Database _db;
Future<Database> get db async {
if (_db != null) {
return _db;
}
_db = await initDB();
return _db;
}
ConfigDatabase._internal();
Future<Database> initDB() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "sagres_relatorios.db");
var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
return theDb;
}
void _onCreate(Database db, int version) async {
await db.execute(
"CREATE TABLE Empresa ("
" EmpresaId INTEGER PRIMARY KEY AUTOINCREMENT,"
" Nome TEXT,"
" ipServidorGestao TEXT,"
" portaServidorGestao TEXT,"
" ipServidorVendas TEXT,"
" portaServidorVendas TEXT,"
" CNPJ TEXT,"
" SenhaREST TEXT,"
" Selecionado BIT);");
print("Tabela Empresa Criada!");
await db.execute(
"CREATE TABLE ConfigAtual ("
" EmpresaId INTEGER,"
" SessaoGuidId TEXT,"
" Operador TEXT,"
" isChecked BIT,"
" FOREIGN KEY(EmpresaId) REFERENCES Empresa(EmpresaId)"
");");
print("Tabela ConfigAtual Criada!");
initPrimeirosDados();
print("Primeiros Dados init");
}
uptade(String Nome, String ipServidorGestao, String portaServidorGestao, String ipServidorVendas, String portaServidorVendas,
String CNPJ, String SenhaREST, int EmpresaId) async {
var dbClient = await db;
int count = await dbClient.rawUpdate(
'UPDATE Empresa SET Nome = ?, ipServidorGestao = ?, portaServidorGestao = ?, ipServidorVendas = ?, portaServidorVendas = ?,'
' CNPJ = ?, SenhaREST = ? WHERE EmpresaId = $EmpresaId',
[Nome, ipServidorGestao, portaServidorGestao, ipServidorVendas, portaServidorVendas, CNPJ, SenhaREST]);
return count;
}
insertEmpresa(String Nome, String ipServidorGestao, String portaServidorGestao,
String ipServidorVendas, String portaServidorVendas, String CNPJ, String SenhaREST) async {
var dbClient = await db;
int count = await dbClient.rawUpdate(
'INSERT INTO Empresa(Nome, ipServidorGestao, portaServidorGestao, ipServidorVendas, portaServidorVendas, CNPJ, SenhaREST) VALUES'
'("$Nome", "$ipServidorGestao", "$portaServidorGestao", "$ipServidorVendas", "$portaServidorVendas", "$CNPJ", "$SenhaREST")');
return count;
}
uptadeSessao(String SessaoGuidId) async {
var dbClient = await db;
int count = await dbClient.rawUpdate(
'UPDATE ConfigAtual SET SessaoGuidId = ?',
[SessaoGuidId]);
return count;
}
updateLembrarDeMim(String Operador) async {
var dbClient = await db;
await dbClient.rawUpdate(
'UPDATE ConfigAtual SET Operador = ?',
[Operador]);
}
updateIsChecked(int isChecked) async {
var dbClient = await db;
await dbClient.rawUpdate(
'UPDATE ConfigAtual SET isChecked = ?',
[isChecked]);
}
updateConfigAtual(int EmpresaId) async {
var dbClient = await db;
await dbClient.rawUpdate(
'UPDATE ConfigAtual SET EmpresaId = ?',
[EmpresaId]);
await dbClient.rawUpdate(
'UPDATE Empresa SET Selecionado = 0');
await dbClient.rawUpdate(
'UPDATE Empresa SET Selecionado = 1 WHERE EmpresaId = ?',
[EmpresaId]);
}
Future<List<Map>> carregarListEmpresas() async{
var dbClient = await db;
List<Map> list = await dbClient.rawQuery('SELECT e.ipServidorGestao, e.portaServidorGestao, e.CNPJ, e.SenhaREST, c.SessaoGuidId '
'FROM Empresa E '
'JOIN ConfigAtual C on e.EmpresaId = c.EmpresaId');
return list;
}
Future<ResultConfig> loadLoginPage() async{
var dbClient = await db;
List<Map> list = await dbClient.rawQuery
('SELECT isChecked, Operador FROM ConfigAtual');
ResultConfig BD = new ResultConfig.fromJson(list[0]);
return BD;
}
initPrimeirosDados() async {
var dbClient = await db;
int res = await dbClient.transaction((txn) async {
await txn.rawInsert(
'INSERT INTO ConfigAtual(EmpresaId, Operador, isChecked) VALUES(1, "Supervisor", 1)');
await txn.rawInsert(
'INSERT INTO Empresa(Nome, ipServidorGestao, portaServidorGestao, ipServidorVendas, portaServidorVendas, CNPJ, SenhaREST, Selecionado) VALUES'
'("Sagres Informática", "192.168.0.1", "999", "20", "999", "00.000.000/0000-00", "48965", 1)');
});
return res;
}
insertCNPJ(String CNPJ, String Descricao) async {
var dbClient = await db;
int res = await dbClient.transaction((txn) async {
await txn.rawInsert(
'INSERT INTO CNPJ(CNPJ, Descricao) VALUES("$CNPJ", "$Descricao")');
});
return res;
}
deleteEmpresa(int id) async {
var dbClient = await db;
int res = await dbClient.transaction((txn) async {
await txn.rawInsert(
"DELETE FROM Empresa WHERE EmpresaId = '$id'");
});
return res;
}
Future closeDb() async {
var dbClient = await db;
dbClient.close();
}
}
class ResultConfig {
ResultConfig({
this.EmpresaId,
this.Nome,
this.ipServidorGestao,
this.portaServidorGestao,
this.ipServidorVendas,
this.portaServidorVendas,
this.CNPJ,
this.SenhaREST,
this.SessaoGuidId,
this.Selecionado,
this.Operador,
this.isChecked
});
int EmpresaId, Selecionado, isChecked;
String Nome, ipServidorGestao, portaServidorGestao, ipServidorVendas, portaServidorVendas, CNPJ, SenhaREST, SessaoGuidId, Operador;
factory ResultConfig.fromJson(Map json) {
//returns a List of Maps
return new ResultConfig(
EmpresaId: json['EmpresaId'],
Nome: json['Nome'],
ipServidorGestao: json['ipServidorGestao'],
portaServidorGestao: json['portaServidorGestao'],
ipServidorVendas: json['ipServidorVendas'],
portaServidorVendas: json['portaServidorVendas'],
CNPJ: json['CNPJ'],
SenhaREST: json['SenhaREST'],
SessaoGuidId: json['SessaoGuidId'],
Selecionado: json['Selecionado'],
Operador: json['Operador'],
isChecked: json['isChecked'],
);
}
}但是,在我关闭并打开应用程序后,它将永远完美运行,直到我删除应用程序并重新安装。
在重新打开应用程序后,我看到了这个屏幕,我的loginPage。
有人知道如何在我第一次打开应用程序时正确运行LoginPage吗?
发布于 2018-07-27 02:41:55
看,当没有创建数据库时,你会得到无限循环,因为initPrimeirosDados() -,你从_onCreate()调用它,从initDB()调用,从get db()调用,然后在initPrimeirosDados()中再次调用这个getter方法。
因此,只需在_onCreate()中内联此函数或将Database db作为参数传递即可
initPrimeirosDados(Database dbClient) async {
int res = await dbClient.transaction((txn) async {
await txn.rawInsert(
'INSERT INTO ConfigAtual(EmpresaId, Operador, isChecked) VALUES(1, "Supervisor", 1)');
await txn.rawInsert(
'INSERT INTO Empresa(Nome, ipServidorGestao, portaServidorGestao, ipServidorVendas, portaServidorVendas, CNPJ, SenhaREST, Selecionado) VALUES'
'("Sagres Informática", "192.168.0.1", "999", "20", "999", "00.000.000/0000-00", "48965", 1)');
});
return res;
}https://stackoverflow.com/questions/51540635
复制相似问题