0

I was trying to add my hospitalId string to my List String. It doesn't work idk why. the log says

I/flutter (28204): The method 'add' was called on null.
I/flutter (28204): Receiver: null
I/flutter (28204): Tried calling: add("8a041434-0703-11eb-adc1-0242ac120002")

my Code was:

 List<String>hospitalIds=List<String>();
 void _addLastSearch(String hospitalId,String hospitalName)
  {
   
    final lastSearching=SearchModel(hospitalId: hospitalId,hospitalName: hospitalName);
    setState(() {
                 recentSearch.add(lastSearching);
                 hospitalIds.add(hospitalId);
      });
    
      print("hospital Id");
      print("============================");
      print(hospitalIds);
      print("============================");
       savedData();
  
   
  }

I have tried to change the list to(it doesn't work):

List<String>hospitalIds=List<String>();
   List<String>hospitalIds=[];
Vince
  • 43
  • 8
  • Where did u declare ```recentSearch``` ? – ikerfah Nov 03 '20 at 19:01
  • @ikerfah the recent Search doesn't matter, the problem happens when I add the "hospitalIds.add(hospitalId);" and somehow it can't add it – Vince Nov 03 '20 at 19:07
  • The error is saying that add is called on null, which means ```recentSearch``` or ```hospitalIds``` is null – ikerfah Nov 03 '20 at 19:20
  • Its 100% for sure is hospitalIds sir, – Vince Nov 03 '20 at 19:24
  • @ikerfah new update, I tried to add it manually "hospitalIds.add("test")" and it works. but the thing is why do I need to add it manually, is there any way to fix this issue> – Vince Nov 03 '20 at 19:26
  • Can you put the code where you call ```_addLastSearch``` ? are you sure ```hospitalId``` is not ```null``` ? try to print the value – ikerfah Nov 03 '20 at 19:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224053/discussion-between-vincent-hartanto-and-ikerfah). – Vince Nov 03 '20 at 19:34
  • 1
    Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt Nov 03 '20 at 20:53

1 Answers1

0

Try to initialize the list like this:

List<String>hospitalIds= [];

Félix Paradis
  • 5,165
  • 6
  • 40
  • 49