Sqlite 颤振:为什么这个函数中的方法不访问数据库变量?

Sqlite 颤振:为什么这个函数中的方法不访问数据库变量?,sqlite,flutter,dart,flutter-dependencies,sqflite,Sqlite,Flutter,Dart,Flutter Dependencies,Sqflite,我有一个名为数据库服务.dart的文件。这个文件有一个包含各种方法的类。第一种方法打开数据库并存储引用,而其他方法则在此数据库上进行包含、更新和删除。问题是其他方法无法看到由第一个方法创建的数据库。我错过了什么 代码如下: import 'dart:async'; import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; import 'counter.dart'; class DatabaseService

我有一个名为
数据库服务.dart
的文件。这个文件有一个包含各种方法的类。第一种方法打开数据库并存储引用,而其他方法则在此数据库上进行包含、更新和删除。问题是其他方法无法看到由第一个方法创建的数据库。我错过了什么

代码如下:

import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'counter.dart';

class DatabaseServices {
  initDatabase() async {
    // Open the database and store the reference.
    final Future<Database> database = openDatabase(
      // Set the path to the database.
      join(await getDatabasesPath(), 'counter_database.db'),
      // When the database is first created, create a table to store counters;
      onCreate: (db, version) {
        // Run the CREATE TABLE statement on the database.
        return db.execute(
          "CREATE TABLE counters(id INTEGER PRIMARY KEY, name TEXT, value INTEGER)",
        );
      },
      // Set the version. This executes the onCreate function and provides a
      // path to perform database upgrades and downgrades.
      version: 1,
    );
  }

    // Define a function that inserts counters into the database.
    Future<void> insertCounter(Counter counter) async {
      // Get a reference to the database.
      final Database db = await database;
      // Insert the Counter into the correct table. Here, if a counter is inserted twice,
      // it replace any previous data.
      await db.insert(
        'counters',
        counter.toMap(),
        conflictAlgorithm: ConflictAlgorithm.replace,
      );
    }
}
    // A method that retrieves all the counters from the counters table.
    Future<List<Counter>> counters() async {
      // Get a reference to the database.
      final Database db = await database;
      // Query the table for all the Counters.
      final List<Map<String, dynamic>> maps = await db.query('counters');
      // Counvert the List<Map<String, dynamic>> into a List<Counter>
      return List.generate(maps.length, (i) {
        return Counter(
          id: maps[i]['id'],
          name: maps[i]['name'],
          value: maps[i]['value'],
        );
      });
    }

    // Method to update a Counter in the database
    Future<void> updateCounter(Counter counter) async {
      final db = await database;
      await db.update(
        'counters',
        counter.toMap(),
        where: "id = ?",
        whereArgs: [counter.id],
      );
    }

    //Delete a Counter from the database
    Future<void> deleteCounter(int id) async {
      final db = await database;
      await db.delete(
        'counters',
        where: "id = ?",
        whereArgs: [id],
      );
    }
}

导入'dart:async';
导入“package:path/path.dart”;
导入“包:sqflite/sqflite.dart”;
输入“计数器.省道”;
类数据库服务{
initDatabase()异步{
//打开数据库并存储引用。
最终未来数据库=openDatabase(
//设置数据库的路径。
join(wait getDatabasesPath(),'counter_database.db'),
//第一次创建数据库时,创建一个表来存储计数器;
onCreate:(数据库,版本){
//在数据库上运行CREATETABLE语句。
返回db.execute(
“创建表计数器(id整型主键、名称文本、值整型)”,
);
},
//设置版本。这将执行onCreate函数并提供
//执行数据库升级和降级的路径。
版本:1,,
);
}
//定义将计数器插入数据库的函数。
未来插入计数器(计数器)异步{
//获取对数据库的引用。
最终数据库db=等待数据库;
//将计数器插入正确的表中。此处,如果计数器插入两次,
//它将替换任何以前的数据。
等待db.insert(
"柜台",,
counter.toMap(),
conflictAlgorithm:conflictAlgorithm.replace,
);
}
}
//从计数器表中检索所有计数器的方法。
未来计数器()异步{
//获取对数据库的引用。
最终数据库db=等待数据库;
//查询表中的所有计数器。
最终列表映射=等待db.query('counters');
//将列表转换为一个列表
返回列表。生成(maps.length,(i){
返回计数器(
id:maps[i]['id'],
名称:maps[i]['name'],
值:映射[i]['value'],
);
});
}
//方法更新数据库中的计数器
未来更新计数器(计数器)异步{
最终数据库=等待数据库;
等待数据库更新(
"柜台",,
counter.toMap(),
其中:“id=?”,
wherergs:[counter.id],
);
}
//从数据库中删除计数器
未来删除计数器(int-id)异步{
最终数据库=等待数据库;
等待db.delete(
"柜台",,
其中:“id=?”,
wherergs:[id],
);
}
}

数据库变量只存储在
initDatabase
方法中,而不是存储在
DatabaseServices
类中,这意味着只能从
initDatabase
方法访问该变量

下面的代码示例显示了如何将
数据库
存储为
DatabaseServices
类上的属性,这将允许该类中的所有方法使用它

类数据库服务{
未来(db);
Future initDatabase()异步{
//打开数据库并存储引用。
_db=开放数据库(
//设置数据库的路径。
join(wait getDatabasesPath(),'counter_database.db'),
//第一次创建数据库时,创建一个表来存储计数器;
onCreate:(数据库,版本){
//在数据库上运行CREATETABLE语句。
返回db.execute(
“创建表计数器(id整型主键、名称文本、值整型)”,
);
},
//设置版本。这将执行onCreate函数并提供
//执行数据库升级和降级的路径。
版本:1,,
);
}
//定义将计数器插入数据库的函数。
未来插入计数器(计数器)异步{
//获取对数据库的引用。
最终分贝=等待分贝;
//将计数器插入正确的表中。此处,如果计数器插入两次,
//它将替换任何以前的数据。
等待db.insert(
"柜台",,
counter.toMap(),
conflictAlgorithm:conflictAlgorithm.replace,
);
}
//从计数器表中检索所有计数器的方法。
未来计数器()异步{
//获取对数据库的引用。
最终分贝=等待分贝;
//查询表中的所有计数器。
最终列表映射=等待db.query('counters');
//将列表转换为一个列表
返回列表。生成(maps.length,(i){
返回计数器(
id:maps[i]['id'],
名称:maps[i]['name'],
值:映射[i]['value'],
);
});
}
//方法更新数据库中的计数器
未来更新计数器(计数器)异步{
最终分贝=等待分贝;
等待数据库更新(
"柜台",,
counter.toMap(),
其中:“id=?”,
wherergs:[counter.id],
);
}
//从数据库中删除计数器
未来删除计数器(int-id)异步{
最终分贝=等待分贝;
等待db.delete(
"柜台",,
其中:“id=?”,
wherergs:[id],
);
}
}
您可以找到有关打开数据库的详细信息