0

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

1 Answer 1

1

Try the code below :

void updatelistideas() async {
    final ideas = await _databaseHelper.getIdeaList();
    setState(() {
      listideas = ideas;
    });
    int i = listideas.length;
    debugPrint('ideaslength: $i listideaslength');
  }

and change this :

itemCount: listideas.length,

to

itemCount: null == listideas ? 0 : listideas.length,
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Lapa Ny Aina Tanjona, Thanks for your answer. getter length error is fixed. can you please explain me why this code works.
Sure! At the beginning, listideas is null,. so if it is null we call the updatelistideas () function and we have to do a setState to rebuild our widget (Showideas) again with the new value of the listideas. Please consider accepting it if my answer solve your issue :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.