Hikari
Hikari

Reputation: 11

Get data from firestore and put into list (flutter)

I have retrieve data from Firestore db and put into list but error occur. Furthermore, the dialog on exception block doesn't trigger at all.

class mDetail extends State<homeDetail>{
bool uniFlag = false;
var resultUni;
  @override
  void initState() {
    super.initState();
    try {
      UniQuery().getUniBasedLocProg(selectedState,selectedProg).then((QuerySnapshot qsUni) {
        uniFlag = true;
        for (int i = 0; i < qsUni.documents.length; i++) {
          resultUni.add(qsUni.documents[i]['name']);
        }
      });
    }
    catch(e){
      print(e.message);
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text('Ops something gone wrong'),
              content: Text('Your email or password is wrong'),
            );
          });
    }
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text('EduGradHomePage'),),
      body: Form(
          child: Column(
            children: <Widget>[

              RaisedButton(
                onPressed: back,
                child: Icon(
                  Icons.backspace
                ),
              ),

              Text(resultUni[0])
            ],
          )
    )
    );
  }
}

this code in other package for uniquery

class UniQuery {
  getUniBasedLocProg(String uniLoc, String uniProg){
    return dbReference.collection('University')
        .where('State', isEqualTo: uniLoc)
        .where('Programme', isEqualTo: uniProg)
        .getDocuments();
  }
}

IT say error NoSuchMethodError The method '[]' was calleed on null seems like the value doesnot add in the list

here my DB in firestore

list Programme data

list University data

list State data

Error log

Upvotes: 0

Views: 137

Answers (2)

Hikari
Hikari

Reputation: 11

after i change to bellow code. i manage to get the value idState and idProg are the id based on the document of collection in my firestore

 class UniQuery {
      getUniBasedLocProg(String uniLoc, String uniProg){
        return dbReference.collection('University')
            .where('State', isEqualTo: dbReference.collection('State')
            .document(idState);)
            .where('Programme', isEqualTo: dbReference.collection('Programme')
            .document(idProg);)
            .getDocuments();
      }
    }

Upvotes: 1

Sonu Saini
Sonu Saini

Reputation: 2054

.getUniBasedLocProg(selectedState,selectedProg) is future callback so it's return data after taking some times.during that build also build your code.so that time it's was null that's why you get an error "The method '[]' was calleed on null".

so before use that variable check that's is null or not.

       resultUni[0] ?? "" // if not null then return value otherwise return "" //

full code ->

   class mDetail extends State<homeDetail>{
   bool uniFlag = false;
   var resultUni;
    @override
    void initState() {
    super.initState();
try {
  UniQuery().getUniBasedLocProg(selectedState,selectedProg).then((QuerySnapshot qsUni) 
    {
    uniFlag = true;
    if(qsUni !=null&&qsUni.documents !=null ){
     for (int i = 0; i < qsUni.documents.length; i++) {

      if(qsUni.documents[i] !=null ){
      resultUni.add(qsUni.documents[i]['name']);
      }
    }
  }
  });
}
catch(e){
  print(e.message);
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Ops something gone wrong'),
          content: Text('Your email or password is wrong'),
        );
      });
   }
 }

Widget build(BuildContext context) {
  return new Scaffold(
   appBar: AppBar(
    title: Text('EduGradHomePage'),),
  body: Form(
      child: Column(
        children: <Widget>[

          RaisedButton(
            onPressed: back,
            child: Icon(
              Icons.backspace
            ),
          ),

          Text(resultUni[0] ?? ""),
        ],
      )
  )
 );
 }
 }

Upvotes: 0

Related Questions