LATEST EDIT 2nd Sept:
I am not getting much traction with this, even with a bounty, so I will try and ask a simpler and more specific question.
So I have reorganised the database in line with Doug's suggestions below, as I cannot otherwise reference the arrays in any way in firebase. So now I have a map of arrays, rather than just arrays. Like so:
ObjectsList > CarsMap (Map)
- sh899873jsa (Array)
0 "Toyota"
1 "Supra"
2 "1996"
3 "$4990"
- hasd823j399 (Array)
0 "Toyota"
1 "Corolla"
2 "2014"
3 "$11990"
- nelaoiwi283 (Array)
0 "Ford"
1 "Territory"
2 "2018"
3 "$35000"
But I don't know how to actually use this structure, as I have never seen this before. I am getting the first error now with the code provided to me by Frank in his answer below, which I have converted to:
final DocumentReference documents = await Firestore.instance.collection('ObjectsList');
DocumentSnapshot snapshot = await documents.get();
Map<String, dynamic> data = snapshot.data;
var loadCarItems = [];
data.forEach((k,v) => {
values = List<String>.from(v as List<String>),
print(values),
if (values[0] == "Toyota") {
loadCarItems.add(values[0]),
},
});
setState(() {
CarItemsArray = loadCarItems;
});
But since I have changed to a map>array structure I am getting an error on this line:
data.forEach((k,v) => {
values = List<String>.from(v as List<String>),
The error being:
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<String>' in type cast
So clearly I need to change this syntax now the structure has changed, but I have no idea how, and can't find anything online.
Previous Information:
I am trying to work out a way to return whole arrays from Firebase so that I can then process the data inside.
For example, I have a document in the database that contains arrays, like so:
ObjectsList > sh899873jsa
0 "Toyota"
1 "Supra"
2 "1996"
3 "$4990"
hasd823j399
0 "Toyota"
1 "Corolla"
2 "2014"
3 "$11990"
nelaoiwi283
0 "Ford"
1 "Territory"
2 "2018"
3 "$35000"
So for each array, I have generated a random key on creation, which is not important. I basically just need to be able to return all the data as separate objects. Ideally, I would like to be able to return "All Toyotas", for example. That's the end game here.
Here is the code I have generated so far based on suggestions by Frank below who got me onto the right path.
From the build wdiget:
Container(
child: StreamBuilder(
stream: Firestore.instance.collection('cars').document('ObjectsList').snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return LoadingAnimationBasic();
}
if (snapshot.data == null) {
return LoadingAnimationBasic();
} else {
return ListView(
shrinkWrap: true,
children: _buildListCards(snapshot),
);
}
},
),
),
The _buildListCards function, simplified so you can see how it works:
_buildStoresList(AsyncSnapshot<DocumentSnapshot> snapshot) {
return snapshot.data.data.values
.map((doc) => doc[0] == "Toyota" ? GestureDetector(
child: Container(
width: MediaQuery.of(context).size.width,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0.0),
),
color: Colors.white70,
elevation: 10,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(2.0),
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 120,
minWidth: 120,
maxHeight: 100,
minHeight: 100,
),
child: Image.network(
'some toyota picture URL',
fit: BoxFit.cover,
),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.5,
child: Padding(
padding: const EdgeInsets.fromLTRB(10, 10, 0, 0),
child: Text(
doc[1],
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
),
),
Container(
width: MediaQuery.of(context).size.width * 0.5,
child: Padding(
padding: const EdgeInsets.fromLTRB(5, 10, 0, 0),
child: Text(
doc[2],
style: TextStyle(
fontSize: 12,
),
),
),
),
],
),
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(5, 40, 0, 0),
child: Text(
doc[3],
style: TextStyle(
fontSize: 14,
),
),
),
],
),
],
),
),
),
onTap: () {
futureTapHandlerHere();
},
) : SizedBox(), )
.toList();
}
So the only thing that remains now is to be able to edit/delete these entries from the database, and I think this has to rely on the unique identifier generated when they are created. I can't see how else to possibly perform this functionality without the identifier, but I don't know how to actually use it, or return it from the database.
Calling snapshot.data() returns you a Map<String, dynamic>. You can loop over the entries in this map, and then get the first child of each (array) value.
So something like this:
List.from(event.snapshot.value as List)
final DocumentReference documents = await Firestore.instance.collection('cars').document('ObjectsList');
DocumentSnapshot snapshot = await documents.get();
Map<String, dynamic> data = snapshot.data();
var cars = [];
data.forEach((k,v) => {
var values = List<String>.from(v as List<dynamic>);
cars.add(values[0]);
})
setState(() {
arrayOfCars = cars
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With