StreamBuilder "sometimes" not refreshing my ListView

Hello Guys, I’m really confused about my StreamBuilder I don’t know if it’s a bug or it’s me. Inside my StreamBuilder I pull an Api Call as a Stream. I define the Stream inside my initState “streamDepartment = postDepartment();” because I can change things on the list via buttons and inside the OnChange of the button I call the setState to make a new streamDepartment Variable “streamDepartment = postDepartment();” (Everything’s down in the Code below)

Here is an Image on how it looks: https://imgur.com/a/jRhXmb0

The Button sends an api call and set the specific data to null.
I debugged I a lot and I know that the error is the StreamBuilder… For some reason if a press on the button (and the Event happens where it’s not updating) and it rebuilds the widget… it’s not updates the Stream. Database say it’s null so … the api call goes through.

I want that my List “always” Updates.

I tested it a lot, so around 10 Changes it only updates 8 to 9 times.

I’ve also tried in the ButtonClick to complete reopen the page with Navigator but it’s the same outcome. Around 10 Changes it refreshes 8 to 9 times.

Here is the code for the api call if it’s needed:

  final response = await http.get('url');
  final jsonresponse = json.decode(response.body);

  List<Department> departments = [];

  for(var d in jsonresponse) {
    Department department = Department(
      id: d["id"].toString(),
      fid: d["fid"].toString(),
      description: d["description"].toString(),
      supervisor: d["supervisor"].toString(),
    );
    departments.add(department);

  }
  
  return departments;
} 

And here is the Code for my StreamBuilder:

@override

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text("Test"),
              ],
            ),
          drawer: DrawerDart(),
          body: new StreamBuilder(
            stream: streamDepartment.asStream(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if(snapshot.connectionState == ConnectionState.waiting) {
                return Container(
                  child: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
              } else {
                if(snapshot.connectionState == ConnectionState.done) {
                  if(snapshot.hasData) {
                    return Flex(
                          direction: Axis.horizontal,
                          children: <Widget>[
                            Expanded(
                              child: ListView.builder(
                                itemCount: snapshot.data.length,
                                itemBuilder: (BuildContext context, int index) {
                                  departmentListLength = snapshot.data.length;
                                  if(snapshot.data[index].supervisor != "null") {
                                    String dropdownSupervisorIndex = snapshot.data[index].supervisor;

                                    int userIdListIndex = userIdList.indexOf(int.parse(dropdownSupervisorIndex));
                                    String userName = userNameList[userIdListIndex];
                                    
                                    dropdownValues[index] = userName;
                                  }

                                  if(snapshot.data[index].fid[0] == "1") {

                                    return Card(
                                      margin: EdgeInsets.all(10),
                                      child: Padding(
                                        padding: EdgeInsets.all(10),
                                        child: Column(
                                          children: <Widget>[
                                            Row(
                                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                              children: <Widget>[
                                                
                                                Text(snapshot.data[index].description),

                                                Row(
                                                  children: <Widget>[
                                                    DropdownButton(
                                                      value: dropdownValues[index],
                                                      icon: Icon(Icons.arrow_downward),
                                                      iconSize: 24,
                                                      elevation: 16,
                                                      style: TextStyle(
                                                        color: Colors.deepPurple
                                                      ),
                                                      underline: Container(
                                                        height: 2,
                                                        color: Colors.deepPurpleAccent,
                                                      ),
                                                      items: userNameList?.map((String value) {
                                                        return new DropdownMenuItem(
                                                          value: value,
                                                          child: Text(
                                                            value, 
                                                            style: TextStyle(
                                                              color: dropdownValues.contains(value) ? Colors.grey : null,
                                                            ),
                                                          )
                                                        );
                                                      })?.toList() ?? Container(child: Center(child: Text("Seite bitte neu laden!"),),),  
                                                      onChanged: (newValue) {
                                                        if(!dropdownValues.contains(newValue)) {
                                                          setState(() {
                                                            dropdownValues[index] = newValue;

                                                            String fid = snapshot.data[index].fid;

                                                            int userIndex = userNameList.indexOf(newValue);
                                                            String userid = userIdList[userIndex].toString();

                                                            setSupervisor(fid, userid);

                                                            streamDepartment = postDepartment();
                                                          });
                                                        }
                                                      },

                                                    ),
                                                    IconButton(
                                                      icon: Icon(Icons.cancel),
                                                      onPressed: () {
                                                        setState(() {
                                                          dropdownValues[index] = null;

                                                          String fid = snapshot.data[index].fid;
                                                          String userid = "null";
                                                          setSupervisor(fid, userid);

                                                          /* Navigator.pushReplacement(
                                                            context,
                                                            new MaterialPageRoute(builder: (context) => new TabControllerDart())
                                                          ).then((onValue) { */
                                                            setState(() {
                                                              streamDepartment = postDepartment(); 
                                                            });
                                                          //}); 
                                                        });
                                                      }
                                                    )
                                                  ],
                                                )   
                                              ],
                                            )
                                          ],
                                        )
                                      )
                                    );
                                  } else {
                                    return Container(
                                    );
                                  }
                                }
                              )
                            )
                          ]
                        ),

At onChange await for api respone before setSate and rebuilding List.

1 Like