I recently started using Flutter for app development. I am trying to make a simple notepad app from scratch as part of the learning assignment. can anyone help me where I am making the mistake or if I am missing any fundamental concept? Thanks in advance, below are the details of the issue.
I wrote a database_helper class and a function to show all elements in the database as a list.
//code from Database_Helper.dart
//get the total number of rows in DB.
Future<int> getCount() async {
Database db = await this.database;
List<Map<String, dynamic>> x =
await db.rawQuery('SELECT COUNT (*) from $ideaTable');
int result = Sqflite.firstIntValue(x);
return result;
}
//get total rows in db as <list <map>>
Future<List<Map<String, dynamic>>> getIdeaMapList() async {
Database db = await this.database;
var result = await db.rawQuery('SELECT * FROM $ideaTable ');
return result;
}
//converting list<map> to list<Ideas> // Ideas being defined class.
Future<List<Idea>> getIdeaList() async {
var ideaMapList = await getIdeaMapList(); // Get 'Map List' from database
int count =
ideaMapList.length; // Count the number of map entries in db table
List<Idea> ideaList = List<Idea>();
// For loop to create a 'todo List' from a 'Map List'
for (int i = 0; i < count; i++) {
ideaList.add(Idea.fromMapObject(ideaMapList[i]));
}
return ideaList;
}
}
the database helper object is created in main file and list is shown as vertical Listview using ShowIdea class as shown below.
class _ShowideasState extends State<Showideas> {
DatabaseHelper _databaseHelper = new DatabaseHelper();
List<Idea> listideas;
@override
Widget build(BuildContext context) {
debugPrint('micheal jackson: listideaslength');
if (listideas == null) {
updatelistideas();
}
int j = listideas.length;
debugPrint('micheal jackson: $j listideaslength');
return Container(
child: ListView.builder(
itemCount: listideas.length,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.green[200],
elevation: 2.0,
child: Column(
children: <Widget>[
Text(this.listideas[position].iTitle),
Text(this.listideas[position].iText),
Text(this.listideas[position].date),
],
),
);
}),
);
}
void updatelistideas() async {
listideas = await _databaseHelper.getIdeaList();
int i = listideas.length;
debugPrint('ideaslength: $i listideaslength');
}
}
the following logs
2020-08-23 20:36:51.866 24126-24169/com.example.myprojet01 I/flutter: micheal jackson: listideaslength
2020-08-23 20:36:51.868 24126-24169/com.example.myprojet01 I/flutter: movieTitle: get database list
2020-08-23 20:36:52.358 24126-24169/com.example.myprojet01 I/flutter: idea list length in db: 5
2020-08-23 20:36:52.358 24126-24169/com.example.myprojet01 I/flutter: ideaslength: 5 listideaslength
but the widget is not rendered and shows a red screen with error 'getter length was called on null'.
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building Showideas(dirty, state: _ShowideasState#193ad):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
Showideas
if the app is hot reloaded without any changes. The following logs add up and the screen turns white and is overflowed.
2020-08-23 20:36:56.041 24126-24169/com.example.myprojet01 I/flutter: micheal jackson: listideaslength
2020-08-23 20:36:56.041 24126-24169/com.example.myprojet01 I/flutter: micheal jackson: 5 listideaslength