数据库类
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite_project/instructors/instructors.dart';
class DatabaseHelper {
static DatabaseHelper _databaseHelper;
static Database _database;
String istrcuctTable = 'instruct_table';
String colID = 'id';
String colName = 'name';
String colPass = 'password';
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 $istrcuctTable($colID INTEGER PRIMARY KEY AUTOINCREMENT, $colName TEXT, '
'$colPass TEXT)');
}
// Insert Operation: Insert a Note object to database
Future<int> insertNote(Instructor instructor) async {
Database db = await this.database;
var result = await db.insert(istrcuctTable, instructor.toMap());
return result;
}
}
Main Class
import 'package:flutter/material.dart';
import 'package:sqflite_project/instructors/instructors.dart';
import 'package:sqflite_project/utils/database_helper.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController nameController = TextEditingController();
TextEditingController passController = TextEditingController();
int result;
Instructor note;
DatabaseHelper _databaseHelper = DatabaseHelper();
String name;
String password;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
child: Column(children: <Widget>[
TextFormField(
controller: nameController,
),
TextFormField(
controller: passController,
),
RaisedButton(
child: Text('Login'),
onPressed: () async {
_databaseHelper.insertNote(note);
})
]),
));
}
}
错误
[ERROR:flutter / lib / ui / ui_dart_state.cc(157)]未处理的异常: NoSuchMethodError:方法“ toMap”在null上调用。 E /颤振( 2893):接收方:空E / flutter(2893):尝试调用:toMap()
我已经创建了数据库并执行插入操作,但是当我调用插入操作时,它给出了一个错误,即在null上调用了InsertNote方法。
答案 0 :(得分:0)
错误表示调用instructor
时instructor.toMap()
为空
因为您没有使用任何值初始化note
,所以现在是null
Instructor note;
在调用以下代码时,实际上是将null
传递给insertNote
onPressed: () async {
_databaseHelper.insertNote(note);
})
你可以做
Instructor note = Instructor();
答案 1 :(得分:0)
您尚未将值设置为note(Instructor),因此note为null,因此,每当您对null调用任何函数时,都会出现此错误(note(null).toMap(); 因此您必须在执行操作之前设置数据
note=Instructor();//set propery as you defined in the Instructor class
而且您还必须等待onPressed as
onPressed: () async {
await _databaseHelper.insertNote(note);
})