fluttermobilesqflite

Flutter: The problem with connecting to the database


I want to connect to a local database. I put it in the assets folder and in pubspec.yaml indicated that " - assets/DB/information.db". But I get a mistake. Please help me figure it out.

enter image description here```

import 'dart:io';
import 'dart:typed_data';
import 'package:first_app/const/const.dart'; 
import 'package:flutter/services.dart';
import 'package:path/path.dart'; 
import 'package:sqflite/sqflite.dart'; 
class DatabaseHelper{
Future<Database> copyDB() async{  
  var dbPath = await getDatabasesPath();
  var path = join(dbPath, db_name);

  var exists = await databaseExists(path);

  if(!exists){
    try{
      await Directory(dirname(path)).create(recursive: true);
    } catch(_){}

    //copy from assets
    ByteData data = await rootBundle.load(join('assets/DB', db_name));
    List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    await Fail(path).writeAsBytes(bytes, flush:true);
  }
  else {
    print('DB already exist');
  }
  return await openDatabase(path,readOnly: true );
}
}

Solution

  • I think you need to change Fail(path) to File(path)