Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get a list from sharedpreferences, and access a list's methods. I made a class for the sharedpreferences, but I am getting an error when doing this:
sharedpreferences().getnom.length;
//or
sharedpreferences().getnom[index];
//errors
//The getter 'length' isn't defined for the class 'Future<List<String>>'.
//Error: The operator '[]' isn't defined for the class 'Future<List<String>>'.


What I have tried:

class sharedprefernces {
  List<String> _depto = [];
  List<String> _mun = [];
  List<String> _com = [];
  List<String> _nom = [];
  List<String> _peso = [];
  List<String> _qq = [];
  List<String> _varie = [];
  List<String> _date = [];
  List<String> _est = [];

  Future<List<String>> getnom() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  return prefs.getStringList('nom');
  }
}
Posted
Updated 23-May-20 22:50pm

1 solution

//The getter 'length' isn't defined for the class 'Future<List<String>>'.
//Error: The operator '[]' isn't defined for the class 'Future<List<String>>'.

The messages are quite clear. Your sharedprefernces class does not have a length property, or an indexer. You need to add those to your class or inherit from a class that does provide them.
 
Share this answer
 
Comments
Member 10850253 24-May-20 12:34pm    
Can you please tell me, how can I do this?
I thought that with my getter, I would get the actual list, and that I would be able to use all the list properties.
Please help me find a solution.
Thanks.
Richard MacCutchan 24-May-20 12:54pm    
You need to write a proper getter method (following the standard rules) or an indexer. If you do not understand how then look at the C# documentation: Using Properties - C# Programming Guide | Microsoft Docs[^] and Indexers - C# Programming Guide | Microsoft Docs[^].
Member 10850253 24-May-20 17:23pm    
From what I understand, I should be able to use the indexer, with the getter, since I am returning a list. I checked the link you gave me, but I still don't understand how to do this. I would really appreciate if you could give me an example on how to do this, in flutter.
Thanks for your help.
Richard MacCutchan 25-May-20 3:07am    
Sorry, I have no idea what flutter is. Also, my apologies but I gave you links to C# code instead of Java; although the code above does not look like Java.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900