Reputation: 1147
In my documents on FireStore, each one has a list of strings. When I'm displaying the document in app, I would like to sort them alphabetically. What I'm trying doesn't work.
var words = document['list'].cast<String>();
words.sort(); // Outputs 'null'
On inspection in the debugger, when I'm casting the list the object is of type CastList
, but I can't find any info on this, and trying to create an object with that declared type tells me that it is an undefined class. So then I tried to specify the class that I'd like it to be:
List<String> words = document['list'].cast<String>();
But it still outputs null
when I try to sort.
I'm getting all of the documents inside of lists
and displaying each of them in a listView.
StreamBuilder(
stream: Firestore.instance.collection('lists').orderBy('releases').snapshots,
builder: (context, snapshot) {
if (!snapshot.hasData)
return const Center(child: Text('Loading...'));
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildRow(context, snapshot.data.documents[index], index),
);
},
)
Widget _buildRow(BuildContext context, DocumentSnapshot document, int index) {
var words = document['list'].cast<String>();
var wordsString = words.toString();
wordsString = wordsString.substring(1, wordsString.length - 1);
return CheckboxListTile(
title: Text(
document['name'],
style: _largerTextStyle,
),
subtitle: Text(
wordsString,
style: _textStyle,
),
value: _selectedIndices.contains(index),
onChanged: (bool value) {
setState(() {
if (value) _selectedIndices.add(index);
else _selectedIndices.remove(index);
});
},
);
}
Upvotes: 1
Views: 2282
Reputation: 103561
It should work , don't need to call cast
.
Edit: I think you forgot to extract the data.
List words = document.data['list'];
words.sort();
Upvotes: 1