无法从SqFLite显示ListView
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法从SqFLite显示ListView相关的知识,希望对你有一定的参考价值。
我的数据能够无任何错误地上传到数据库,但是我似乎无法显示带有数据的列表视图。
[如从_submit()
功能中看到的,如果出现错误,则会显示快餐栏,指示存在错误,并且不会进入主页,但是,结果显示带有成功消息的快餐栏,
所以我怀疑这与我的listview代码或我的databasehelper有关,因为我可能错过了代码中的某些内容。任何帮助深表感谢!
这里是我的列表视图代码:
FutureBuilder<List<Note>>(
future: _databaseHelper.getNoteList(),
builder: (BuildContext context, AsyncSnapshot<List<Note>> snapshot)
if(snapshot.hasData)
return ListView.builder(
itemCount: _count,
itemBuilder: (BuildContext context, int position)
Note note = snapshot.data[position];
return Card(
color: Colors.white,
elevation: 2.0,
child: new ListTile(
title: new Text(note.title),
subtitle: new Text(note.bodyText),
onTap: () =>
_navigateToEditAddPage(note, 'Edit a Note'),
onLongPress: () => _showDeleteDialog(note),
),
);
);
else
return Container(width: 0,height: 0,);
,),
这里是我的insertData代码:
void _submit() async
if (_formKey.currentState.validate())
note.title = _titleController.text;
note.bodyText = _bodyTextController.text;
note.date = _dateController.text;
if (note.id == null)
int result = await _databaseHelper.insertData(note);
if (result != 0)
_moveToHomePage('Note successfully added');
else
_showSnackBar('Note unable to be inserted due to some error');
else
int result = await _databaseHelper.updateData(note);
if (result != 0)
_moveToHomePage('Note successfully updated');
else
_showSnackBar('Note unable to be updated due to some error');
这里是我的DatabaseHelper代码:
class DatabaseHelper
static Database _database;
String dataTable = 'NoteTable';
String colId = 'id';
String colTitle = 'title';
String colBody = 'bodyText';
String colDate = 'date';
DatabaseHelper._();
static final DatabaseHelper db = DatabaseHelper._();
Future<Database> get database async
if (_database == null)
_database = await initializeDatabase();
return _database;
Future<Database> initializeDatabase() async
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'notes.db';
var notesDatabase =
await openDatabase(path, version: 1, onCreate: _createDb);
return notesDatabase;
void _createDb(Database database, int newVersion) async
await database.execute("CREATE TABLE $dataTable ("
"$colId INTEGER PRIMARY KEY AUTOINCREMENT,"
"$colTitle TEXT,"
"$colBody TEXT,"
"$colDate TEXT"
")");
Future<List<Map<String, dynamic>>> getNoteListMap() async
Database db = await this.database;
var result = await db.query(dataTable);
return result;
Future<int> insertData(Note note) async
Database db = await this.database;
var result = await db.insert(dataTable,note.toMap(),conflictAlgorithm:
ConflictAlgorithm.replace,);
return result;
Future<int> updateData(Note note) async
Database db = await this.database;
var result = await db.update(dataTable,note.toMap(),
where: 'colId = ?', whereArgs: [note.id]);
return result;
Future<int> deleteData(Note note) async
Database db = await this.database;
var result = await db
.delete(dataTable, where: 'colId = ?', whereArgs: [note.id]);
return result;
Future<int> getCount() async
Database db = await this.database;
List<Map<String,dynamic>> x = await db.rawQuery('SELECT COUNT (*) from $dataTable');
int result = Sqflite.firstIntValue(x);
return result;
Future<List<Note>> getNoteList() async
var noteMapList = await getNoteListMap();
int count = noteMapList.length;
//list of notes, each note consist of their own independent variables
List<Note> noteList;
for (int i = 0; i < count; i++)
noteList.add(Note.fromMapObject(noteMapList[i]));
return noteList;
最后是我的Note模型:
class Note
int _id;
String _date;
String _title;
String _bodyText;
Note(this._date, this._title, this._bodyText);
Note.withId(this._id, this._date, this._title, this._bodyText);
set date(String date)
this._date = date;
get date => _date;
set title(String title)
this._title = title;
get title => _title;
set bodyText(String bodyText)
this._bodyText = bodyText;
get bodyText => _bodyText;
get id => _id;
Map<String, dynamic> toMap()
var map = new Map<String, dynamic>();
if (_id != null)
map['id'] = _id;
map['title'] = _title;
map['bodyText'] = _bodyText;
map['date'] = _date;
return map;
//Converting a map object to a note object
Note.fromMapObject(Map<String,dynamic> fromMap)
_id = fromMap['id'];
_title = fromMap['title'];
_bodyText = fromMap['bodyText'];
_date = fromMap['date'];
答案
我在您的代码中发现两个错误。
1:在DatabaseHelper的getNoteList()中
List<Note> noteList;
to
List<Note> noteList = [];
2:在列表视图代码中
itemCount: _count,
to
itemCount: snapshot.data.length,
结果:
以上是关于无法从SqFLite显示ListView的主要内容,如果未能解决你的问题,请参考以下文章
(更新:如何在 sqflite 中存储 List<String> 数据类型) sqflite 错误:DatabaseException(java.lang.String 无法转换为 jav