I am trying to retrieve array "class" as shown below in picture using streambuilder. I tried same using future builder but didn't find proper solution from question asked earlier. How can i retrieve the list of class and display in listview?
The error is at snapshot.data['fields']['class'][position]
when i hover over ['fields'] showing:
The argument type 'String' can't be assigned to the parameter type 'int'
I implemented following code using streambuilder:
var ref;
List values = [];
Future<void> getassignment() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final name = prefs.getString('orgname') ?? '';
print(name);
var ref = FirebaseFirestore.instance.collection('Org').doc(name).collection('Login').doc(FirebaseAuth.instance.currentUser.uid);
print(ref);
ref.snapshots().forEach((doc) {
values = List.from(doc.data()['fields']['class']);
print(values);
});
}
@override
void initState() {
getassignment();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF1976D2),
body: StreamBuilder<List<DocumentSnapshot>>(
stream: ref.snapshots(),
builder: (context,snapshot){
if(!snapshot.hasData){
print("Error");
}else{
return Container(
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, position) {
return GestureDetector(
onTap: (){
Navigator.of(context).push(MaterialPageRoute<Null>(
builder: (BuildContext context){
return new SubjectList(
clas:snapshot.data['fields']['class'][position].toString(),
);
}
));
},
child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(snapshot.data['fields']['class'][position].toString(), style: TextStyle(fontSize: 22.0),),
),
),
);
},
),
);
}
},
)
);
}