3

我有一个使用 sqflite 数据库的颤振应用程序。它可以在 android 模拟器和设备上完美运行,也可以在 ios 模拟器上运行。在 ios 真实设备上它可以工作,但不能保存或检索数据,只是静态空 UI 我使用 xcode 10.1,在 iphone 6 上使用 ios 11.2.6,在 iphone 5s 上使用 ios 12.1.4。

    import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_app/models/note.dart';

class DatabaseHelper {

    static DatabaseHelper _databaseHelper;    // Singleton DatabaseHelper
    static Database _database;                // Singleton Database

    String noteTable = 'note_table';
    String colId = 'id';
    String colTitle = 'title';
    String colDescription = 'description';
    String colPriority = 'priority';
    String colDate = 'date';

    DatabaseHelper._createInstance(); // Named constructor to create instance of DatabaseHelper

    factory DatabaseHelper() {

        if (_databaseHelper == null) {
            _databaseHelper = DatabaseHelper._createInstance(); // This is executed only once, singleton object
        }
        return _databaseHelper;
    }

    Future<Database> get database async {

        if (_database == null) {
            _database = await initializeDatabase();
        }
        return _database;
    }

    Future<Database> initializeDatabase() async {
        // Get the directory path for both Android and iOS to store database.
        Directory directory = await getApplicationDocumentsDirectory();
        String path = directory.path + 'notes.db';

        // Open/create the database at a given path
        var notesDatabase = await openDatabase(path, version: 1, onCreate: _createDb);
        return notesDatabase;
    }

    void _createDb(Database db, int newVersion) async {

        await db.execute('CREATE TABLE $noteTable($colId INTEGER PRIMARY KEY AUTOINCREMENT, $colTitle TEXT, '
                '$colDescription TEXT, $colPriority INTEGER, $colDate TEXT)');
    }

    // Fetch Operation: Get all note objects from database
    Future<List<Map<String, dynamic>>> getNoteMapList() async {
        Database db = await this.database;

//      var result = await db.rawQuery('SELECT * FROM $noteTable order by $colPriority ASC');
        var result = await db.query(noteTable, orderBy: '$colPriority ASC');
        return result;
    }

    // Insert Operation: Insert a Note object to database
    Future<int> insertNote(Note note) async {
        Database db = await this.database;
        var result = await db.insert(noteTable, note.toMap());
        return result;
    }

    // Update Operation: Update a Note object and save it to database
    Future<int> updateNote(Note note) async {
        var db = await this.database;
        var result = await db.update(noteTable, note.toMap(), where: '$colId = ?', whereArgs: [note.id]);
        return result;
    }

    // Delete Operation: Delete a Note object from database
    Future<int> deleteNote(int id) async {
        var db = await this.database;
        int result = await db.rawDelete('DELETE FROM $noteTable WHERE $colId = $id');
        return result;
    }

    // Get number of Note objects in database
    Future<int> getCount() async {
        Database db = await this.database;
        List<Map<String, dynamic>> x = await db.rawQuery('SELECT COUNT (*) from $noteTable');
        int result = Sqflite.firstIntValue(x);
        return result;
    }

    // Get the 'Map List' [ List<Map> ] and convert it to 'Note List' [ List<Note> ]
    Future<List<Note>> getNoteList() async {

        var noteMapList = await getNoteMapList(); // Get 'Map List' from database
        int count = noteMapList.length;         // Count the number of map entries in db table

        List<Note> noteList = List<Note>();
        // For loop to create a 'Note List' from a 'Map List'
        for (int i = 0; i < count; i++) {
            noteList.add(Note.fromMapObject(noteMapList[i]));
        }

        return noteList;
    }

}

此代码是 youtube 上的教程,仅适用于 android 和 ios 模拟器

4

3 回答 3

5

我在github 上找到了这个答案

import 'package:path/path.dart' as p;

Future<Database> initializeDatabase() async {
    //Get path of the directory for android and iOS.

    var databasesPath = await getDatabasesPath();
    String path = p.join(databasesPath, 'cards.db');

    //open/create database at a given path
    var cardDatabase = await openDatabase(path, version: 1, onCreate: _createDb);

    return cardDatabase;

  }

在 pubspec.yaml 中添加包

dependencies:
  flutter:
    sdk: flutter
  path: any
于 2019-03-20T11:41:33.377 回答
1

I run into same issue. You may use path_provider package but then, for ios, you also have to use path_provider_macos package

path_provider: ^1.6.1
path_provider_macos: ^0.0.1

I also switched to path package.

于 2020-02-26T10:17:57.607 回答
0

首先从 yaml 文件中删除 path_provider 并添加

  path: any

进入你的 .yaml 文件。现在在数据库助手类中添加这个导入

import 'package:path/path.dart' as p;

并创建一个像这样的数据库路径

Future<Database> initializedDatabase() async {

var directory = await getApplicationDocumentsDirectory();
String path = p.join(directory.toString(),"imo.db");

/// create the database
var notesDatabase = await openDatabase(path, version: 1, onCreate: _createDatabase);
return notesDatabase;
 }
于 2020-11-01T13:22:41.033 回答