我正在 Flutter App 中练习 SQFLite 数据库。我想创建一个数据库,用户可以在其中添加朋友的姓名和手机号码。
添加此信息后,该信息列表将显示在提交按钮 [ 我使用 listview.builder()
的位置] 下。
一切正常。数据正在添加到数据库中。 (我也手动检查了 .db 文件)。但是当我刷新列表时,它向我显示了这样的错误 -
flutter: Done Fetch
[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
#0 QueryResultSet.columnIndex (package:sqflite_common/src/collection_utils.dart:114:32)
#1 QueryRow.[] (package:sqflite_common/src/collection_utils.dart:135:40)
#2 new Contact.fromMap (package:sample/model/contact.dart:25:15)
#3 DatabaseHelper.fetchContacts.<anonymous closure> (package:sample/utils/database_helper.dart:63:39)
#4 MappedListIterable.elementAt (dart:_internal/iterable.dart:412:31)
#5 ListIterator.moveNext (dart:_internal/iterable.dart:341:26)
#6 new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
#7 new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#8 new List.of (dart:core-patch/array_patch.dart:50:28)
#9 ListIterable.toList (dart:_internal/iterable.dart:212:44)
#10 DatabaseHelper.fetchContacts (package:sample/utils/database_helper.dart:63:51)
<asynchronous suspension>
数据获取功能也完美运行。 (控制台向我展示 print("Done Fetch");
的输出)。我认为刷新功能有问题。
请有人帮助我。问题出在哪里,我该如何解决?
<块引用>UI 屏幕 =>
import 'package:flutter/material.dart';
.....
class FlutterLocalDatabase extends StatefulWidget {
@override
_FlutterLocalDatabaseState createState() => _FlutterLocalDatabaseState();
}
class _FlutterLocalDatabaseState extends State<FlutterLocalDatabase> {
....
Contact _contact = Contact();
List<Contact> contactList = [];
DatabaseHelper _dbHelper;
@override
void initState() {
super.initState();
setState(() {
_dbHelper = DatabaseHelper.instance;
});
refreshContactList();
}
@override
Widget build(BuildContext context) {
double _width = MediaQuery.of(context).size.width;
double _height = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar(
title: Text("Flutter SQFLite Database"),
),
body: GestureDetector(
......
.....
ElevatedButton(
onPressed: () async {
print("${_contact.name}, ${_contact.mobile}");
if (validateAndSave(_formKey)) {
FocusScope.of(context).requestFocus(new FocusNode());
if (_contact.id == null) {
await _dbHelper.insertContact(_contact).then((value) {
print(value);
_resetForm();
refreshContactList();
});
}
print(contactList);
}
},
child: Text("Submit")),
SizedBox(
height: 20,
),
Expanded(
child: Container(
child: ListView.builder(
itemCount: contactList.length,
itemBuilder: (buildContext, index) {
var contact = contactList[index];
return Card(
child: ListTile(
leading: Icon(
Icons.account_circle,
size: 40,
color: Colors.teal,
),
title: Text(
contact.name,
style: TextStyle(
color: Colors.teal,
),
),
subtitle: Text(contact.mobile,
style: TextStyle(
color: Colors.teal,
)),
trailing: Text("${contact.id}",
style: TextStyle(
color: Colors.teal,
)),
),
);
}),
))
],
),
),
),
),
);
}
refreshContactList() async {
List<Contact> x = await _dbHelper.fetchContacts();
print("Done Refresh");
setState(() {
contactList = x;
});
}
_resetForm() {
setState(() {
_formKey.currentState.reset();
_ctrlName.clear();
_ctrlMobile.clear();
_contact.id = null;
});
}
}
<块引用>
模型类 =>
class Contact {
static const tblContact = "contacts";
static const colId = "id";
static const colName = "name";
static const colMobile = "mobile";
Contact({
this.id,
this.name,
this.mobile,
});
int id;
String name;
String mobile;
Map<String, dynamic> toMap() {
Map map = <String, dynamic>{colName: name, colMobile: mobile};
if (id != null) map[colId] = id;
return map;
}
Contact.fromMap(Map<String, dynamic> map) {
id = map[colId];
name = map[name];
mobile = map[colMobile];
}
}
<块引用>
数据库助手类 =>
import 'dart:io';
.....
class DatabaseHelper {
static const _databaseName = "ContactData.db";
static const _databaseVersion = 1;
//<====== Singleton Class
DatabaseHelper._();
static final DatabaseHelper instance = DatabaseHelper._();
Database _database;
Future<Database> get database async {
if (_database != null) {
return _database;
} else {
_database = await _initDatabase();
return _database;
}
}
//CREATE DATABASE
_initDatabase() async {
Directory dataDirectory = await getApplicationDocumentsDirectory();
String dbPath = join(dataDirectory.path, _databaseName);
print(dbPath);
return await openDatabase(dbPath,
version: _databaseVersion, onCreate: _onCreate);
}
//CREATE TABLE
_onCreate(Database db, int version) async {
db.execute('''
CREATE TABLE ${Contact.tblContact}(
${Contact.colId} INTEGER PRIMARY KEY AUTOINCREMENT,
${Contact.colName} STRING NOT NULL,
${Contact.colMobile} STRING NOT NULL
);
''');
print("Done on Create");
}
//<=================== ADD DATA
Future<int> insertContact(Contact contact) async {
Database db = await database;
print("Done on Insert");
return await db.insert(Contact.tblContact, contact.toMap());
}
//<==================== Read Data
Future<List<Contact>> fetchContacts() async {
Database db = await database;
List<Map> contacts = await db.query(Contact.tblContact);
print("Done Fetch");
return contacts.length == 0
? []
: contacts.map((x) => Contact.fromMap(x)).toList();
}
}
答案 0 :(得分:0)
The map index operator is nullable
这意味着如果没有找到索引,它可以返回 null 因此你应该让你的模型属性可以为 null
class Contact {
...
int? id;
String? name;
String? mobile;