1

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'

enter image description here

enter image description here

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),),
                  
                ),
              ),
            );
          },
        ),
           );
         
          }
        },
      )
          
        
    );
  }

Mrunal Joshi
  • 367
  • 1
  • 4
  • 11

1 Answers1

1

The data in your class array is in fact a string, not an integer. You can tell because it has quotes around the value. If it was a number type value, it would not have quotes.

If you need a number there, you code that writes the data should use a number type variable, not a String. You might have to convert it to a number first.

If you can't change the code that writes the document, then you will have to change the code that reads the database to convert the string to a number.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441