I have a futurebuilder that calls an API connected to DynamoDB. When I remove an item from dynamoDB however it doesn't update my list unless I navigate away from the screen and then come back, allowing the FutureBuilder itself to rebuild.
I've tried using a ListView with a variable calling the API on init state and modifying that variable locally while still on the same view, but that had its own complications.
Flexible where FutureBuilder is used.
Flexible(
flex: 31,
child: FutureBuilder<UserList>(
future: getUsers(URL+GroupID),
builder: (context, snapshot) {
if (snapshot.hasData) {
print("HAS DATA");
if (snapshot.data.users == null) {
return Column(
children: <Widget>[
Padding(
child: Text(
"You have no users",
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
textAlign: TextAlign.center,
),
padding: EdgeInsets.fromLTRB(10, 50, 10, 10),
),
Icon(FontAwesomeIcons.solidSadTear, size: 80, color: Colors.white,),
],
);
} else {
return ListView.builder(
itemCount: snapshot.data.users.length,
itemBuilder: (BuildContext context, int i) {
return Slidable(
actionPane: SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
child: new UserView(snapshot.data.users[i], GroupID),
actions: <Widget>[
IconSlideAction(
caption: 'Delete',
color: Colors.redAccent,
icon: Icons.delete_forever,
onTap: () => {
removeUser(snapshot.data.users[i])
},
),
],
);
},
padding: EdgeInsets.only(),
);
}
} else if (snapshot.hasError) {
showAlert(context, "Failure to get Item",
"Please check DynamoDB");
return Center(child: Text("App Failure\nPlease restart the app"));
} else {
return Center(child: CircularProgressIndicator());
}
},
),
),
removeUser Method: gets a current list, removes specified user and then updates Dynamo.
removeUser(UserObject user) async{
UserList userList = await getUsers(URL+GroupID);
var i = 0;
for(var temp in userList.users){
if(temp.UID == user.UID){
userList.users.removeAt(i);
String update = await updateAdminUsers(URL+"updateUser/"+GroupID, userList);
setState(() {});
}
i++;
}
}
What I'm wanting it to ultimately do is update the view without me needing to navigate away and come back. By using SetState(){} it updates the length of the list, but the list itself never updates. Example: If the list has bill and bob and I delete bill from the list, bob will get deleted since he is the last element in the list and bob stays. When you navigate away and come back though it has the correct list there, however. Dynamo has no issues, but the FutureBuilder won't update.