Android 如何将现有的sqlite数据库添加到Flatter并作为卡片列表打印?

Android 如何将现有的sqlite数据库添加到Flatter并作为卡片列表打印?,android,database,sqlite,flutter,dart,Android,Database,Sqlite,Flutter,Dart,我一直在寻找解决我问题的办法。许多人已经写了一些关于将现有的SQLite数据库添加到Flatter的文章,但没有一篇是准确的 所以我的问题是如何在我的flatter应用程序中添加一个现有的SQLite数据库(名为products.db)。我已经创建了产品的模型类,并添加了一个资产文件夹,其中包含products.db文件,当然,我已经用资产编辑了pubspec.yaml。 这是我的产品。dart型号类别: class Products { int _pid; int _cid; int _ti

我一直在寻找解决我问题的办法。许多人已经写了一些关于将现有的SQLite数据库添加到Flatter的文章,但没有一篇是准确的

所以我的问题是如何在我的flatter应用程序中添加一个现有的SQLite数据库(名为
products.db
)。我已经创建了产品的模型类,并添加了一个资产文件夹,其中包含
products.db
文件,当然,我已经用资产编辑了
pubspec.yaml
。 这是我的
产品。dart
型号类别:

class Products {

int _pid;
int _cid;
int _tid;
String _model;
String _description;
String _pictureURI;
double _price;

// Construktor
  Products(this._pid, this._cid, this._tid , this._model, this._description, this._pictureURI, 
  this._price);

// getters
int get id => _pid;
int get cid => _cid;
int get tid => _tid;
String get model => _model;
String get description => _description;
String get pictureURI => _pictureURI;
double get price => _price;

// setters dont needed

// Extract a Product Object from a Map Oject
Products.fromMapOject(Map <String,dynamic> map) {

this._pid = map['pid'];
this._cid = map ['cid'];
this._tid = map ['tid'];
this._model = map ['model'];
this._description = map ['description'];
this._pictureURI = map ['pictureURI'];
this._price = map ['price'];

 }

}
类产品{
内部pid;
国际刑事法院;
国际工业贸易署;
弦模型;
字符串描述;
字符串_pictureURI;
双倍价格;
//承包商
产品(本._pid,本._cid,本._tid,本._型号,本._说明,本._图片),
这是价格);
//吸气剂
int get id=>\u pid;
int-get-cid=>\u-cid;
int get tid=>\u tid;
字符串get model=>\u model;
字符串get description=>\u description;
字符串get pictureURI=>\u pictureURI;
双倍获取价格=>\u价格;
//二传手不需要
//从地图项目中提取产品对象
产品。FromMapProject(地图){
这个._pid=map['pid'];
这个._cid=map['cid'];
这个._tid=map['tid'];
这个._model=map['model'];
这个._description=map['description'];
这个._pictureURI=map['pictureURI'];
这个._price=map['price'];
}
}

@Soner624,假设您按照说明进行操作,并且拥有products.db的实例

  • 创建数据库函数以获取您的产品(我没有您的数据库信息,所以下面只是一个示例,让您了解一下。产品\u表\u名称=您的表名,列\u产品\u ID=您的产品ID列名)

希望这有帮助。祝你好运

你试过用吗?试过这个。在输出终端上写着“打开现有数据库”,所以我想在这一点上还可以,但是我如何读取并将其保存在列表卡中以显示列表中的值呢?这就是问题所在让我知道答案是否对你有效。
// Get product based on id
Future<Products> getProduct(Database assetDB, int id) async {
    final db = assetDB;
    var res = await db.query(PRODUCTS_TABLE_NAME, where: COLUMN_PRODUCT_ID + " = ?", whereArgs: [id]);
    Products product = res.isNotEmpty ? Products.fromMap(res.first) : null;
    return product;
}

// Get all products
Future<List<Products>> getAllProducts(Database assetDB) async {
    final db = assetDB;
    var res = await db.query(PRODUCTS_TABLE_NAME);
    List<Products> list =
    res.isNotEmpty ? res.map((c) => Products.fromMap(c)).toList() : [];
    return list;
}
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: getAllProducts(assetDB),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Column(
              children: List.generate(snapshot.data.length, (itemIndex) {
                Products product = snapshot.data[itemIndex];
                return Card(
                  child: ListTile(
                    leading: SizedBox(height: 50.0, width: 50.0, child: NetworkImage(product._pictureURI)),
                    title: Text(product._model),
                    subtitle: Text(product._description + ', ' + product._price),
                ));
              }),
            );
          } else {
            return Center(child: CircularProgressIndicator());
          }
        });
  }