当尝试在我的 flutter 项目中添加本地数据库 sqflite 并尝试在 crome(web) 上运行时,它给了我一个错误

问题描述 投票:0回答:1

今天我尝试将数据库 SQFlite 添加到我的 flutter 项目中并在网络上运行它,但出现错误

根据实现,Ui 很好,但是当我单击 插入按钮 我在项目中定义的用于插入数据的内容时,我发现了一个错误 ->

DartError:MissingPluginException(在通道plugins.flutter.io/path_provider上找不到方法getApplicationDocumentsDirectory的实现)

dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3 throw_

packages/flutter/src/services/platform_channel.dart 320:7

第一个 home.dart 文件

第一个 home.dart 文件

import 'package:flutter/material.dart';
import 'package:local_database/db_handler.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("SQFLite Database"),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: ()async{
      await DBHandler().insertData();
       // final data= await DBHandler().readData();
       // print(data);
          },child:  Text('Insert'),
        ),
      ),
    );
  }
}

我创建数据库并在其中插入值的第二个文件

import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

class DBHandler{
  Database? _database;
// use get because it is getter method
  Future<Database?> get database  async{
    if(_database!=null){
      // means databse pahle se data he to yahi database ko return kar de
      // without creting and path findng

      return _database;
    }
    //path providing
    Directory directory =  await getApplicationDocumentsDirectory();
    // join comes from path import
    // here a named mydatabase.db name file will crete
    String path=   join(directory.path,'mydatabase.db');
    // now open db file and crete table
    _database = await openDatabase(path,version: 1,onCreate: (db, version) {

      // execute means creating the table
      db.execute(
        '''
        CREATE TABLE databasetable(
        id INTEGER PRIMARY KEY ,
        name TEXT ,
         age INTEGER
        '''
      );
     },);
    return _database;
  }
insertData()async{
    // the upper databse got store in db future wala
   Database? db= await database;
   db!.insert('databasetable',{
     'id':1,
     'name': 'John',
     'age': 18,
   });

}

readData() async{
  Database? db= await database;
  // for reading we use query
  // here data will comes in the form of list
  final list = await db!.query('databasetable');
  return list;
}


}

我尝试运行时发现的错误

enter image description here

database flutter cross-platform sqflite local-database
1个回答
0
投票

sqflite
包仅支持Android、iOS和macOS平台,不兼容Flutter web。您可以在其 pub.dev 页面上找到此信息。

对于跨平台支持,包括 Flutter Web,您可以使用

idb_sqflite
包。在其 pub.dev 页面上查看。

© www.soinside.com 2019 - 2024. All rights reserved.