skip to Main Content

I have a textAheadField and successfully get data from it, i call setState so the data can be saved locally in statefullwidget. and i want to store it in database firestore but inside the update method firestore the variable that i want (imageuniversitycollege) is empty and has not been update like in the setstate should be.

This is the textAheadField

  String imageuniversitycollege = "";

  Widget CollegeBuildTextAhead() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 20, vertical: 8),
      child: TypeAheadField<SchoolData?>(
        hideSuggestionsOnKeyboardHide: true,
        debounceDuration: Duration(milliseconds: 500),
        textFieldConfiguration: TextFieldConfiguration(
          controller: collegeAheadController,
          decoration: InputDecoration(
            prefixIcon: Icon(Icons.school),
            border: OutlineInputBorder(),
            hintText: 'Search your school',
          ),
        ),
        suggestionsCallback: SchoolApi.getUserSuggestions,
        itemBuilder: (context, SchoolData? suggestion) {
          final datasugestion = suggestion!;
          return ListTile(
            leading: Container(
              width: 40,
              height: 40,
              child: Image.network(
                datasugestion.image,
                fit: BoxFit.cover,
              ),
            ),
            // leading for image
            title: Text(datasugestion.university),
          );
       },
       onSuggestionSelected: (SchoolData? choose) {
          final datasugestion = choose!;
          collegeAheadController.text = datasugestion.university; //this works fine in the update
          final String imageuniversitycollege = datasugestion.image;
          setState(() {
            final String imageuniversitycollege = datasugestion.image;// this is the data i want
            print(imageuniversitycollege); //i print it in here its get the usual link of image
          });
        },
      ),
    );
  }

The usual elevated button

Center(
                                child: ElevatedButton(
                                  child: const Text(
                                    "Confirm",
                                  ),
                                  onPressed: () {
                                    updateEducationCollege();
                                  },
                                ),
                              )

this is the method update, it works but the image is not filled

Future updateEducationCollege() async {
    try {
      print(FirebaseAuth.instance.currentUser?.uid);
      await FirebaseFirestore.instance
          .collection("education")
          .doc(FirebaseAuth.instance.currentUser!.uid)
          .set({
        "uid": FirebaseAuth.instance.currentUser?.uid,
        "College": collegeAheadController.text,
        "imageCollege": imageuniversitycollege,
      }).then((value) => print("Data changed successfully"));
    } on FirebaseException catch (e) {
      Utils.showSnackBar(e.message);
    }
  }

The collegeAheadController.text seems fine still successfully retrieve the data like the image bellow

display firestore

what should i do? to get this data??

display debug console

2

Answers


  1. Just change

    setState(() {
                final String imageuniversitycollege = datasugestion.image;
              });
    

    to

    setState(() {
                imageuniversitycollege = datasugestion.image;
              });
    

    Instead of updating the existing state variable, you are creating a new local variable. Thats the issue.

    Happy coding:)

    Login or Signup to reply.
  2. When you try update your variable you are define new one, change your onSuggestionSelected to this:

    onSuggestionSelected: (SchoolData? choose) {
              final datasugestion = choose!;
              collegeAheadController.text = datasugestion.university;
              final String imageuniversitycollege = datasugestion.image;
              setState(() {
                imageuniversitycollege = datasugestion.image; //<-- change this
                print(imageuniversitycollege); 
              });
            },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search