Reputation: 193
I have a collection in Firestore. It has a field array i am trying to get the array from Firestore and assign it to a list in flutter. My collection is as below
My code for getting data from Firestore
List<Offset> pointlist = <Offset>[];
getdata() async{
await Firestore.instance.collection("points").document('biZV7cepFJA8T6FTcF08').get().then((value){
setState(() {
List<Offset> pointlist = List.from(value.data['point']);
});
});
}
@override
void initState() {
super.initState();
getdata();
}
i get this error type 'String' is not a subtype of type 'Offset'
Upvotes: 5
Views: 19892
Reputation: 24912
Lets assume the structure to be.
...
arrayOfString
|_element1
|_element2
|_element3
...
Use the following code to get the array elements of arrayOfString
parameter
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return const Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}
Map<String, dynamic> data =
snapshot.data!.data()! as Map<String, dynamic>;
return ListView(
children: data['arrayOfString'].map<Widget>((e) { 👈 arrayOfString has array of Strings
return ListTile(
title: Text(e.toString()), // 👈 printing every string
);
}).toList());
},
),
),
);
}
Upvotes: 0
Reputation: 425
you have to declare list and all list item will be store in declared list so you can access it.
List<dynamic> alldata =[];
Future<QuerySnapshot?> getData() async {
dataofItem = FirebaseFirestore.instance
.collection('$data')
.get()
.then((QuerySnapshot? querySnapshot) {
querySnapshot!.docs.forEach((doc) {
allData = doc["item_text_"];
print("allData = $allData");
// print("getData = ${doc["item_text_"]}");
});
});
return dataofItem;
}
Upvotes: 1
Reputation: 8978
The thing which you are doing wrong is this:
// You have initialised your List as a Offset Object Type
List<Offset> pointList;
Secondly, the data you are assigning is a
String
, if you closely take a look at that firebase.
"Offset(x,y)"
Finally, trying to assign the
String
value to aList
of typeOffset class/object
If you want to make the thing works, then either make the
List
of typeString
and then add it to theList
List<String> pointlist = List.from(value.data['point']);
Or first Add the data to the Offset Object like this, and then pass it to the List
List<Offset> pointList = <Offset>[];
getdata() async{
await Firestore.instance.collection("points").document('biZV7cepFJA8T6FTcF08').get().then((value){
setState(() {
// first add the data to the Offset object
List.from(value.data['point']).forEach((element){
Offset data = new Offset(element);
//then add the data to the List<Offset>, now we have a type Offset
pointList.add(data);
});
});
});
}
SUMMARY
Always look for the data type you are giving to the List
, if you are trying to add the data which is not a type T
of List<T>
, you will always get this error of type mismatch. Hope this will give you some clarity and some basic idea about programming. Keep learning :)
Upvotes: 6