I am facing a problem in my current project for rebuilding future builder whenever data is added or updated..
after adding a number into numbers, future builder should be rebuild to show all updated numbers..
i have wrapped Future builder inside Obx but its not rebuilding as it does not contain any obs variable inside it…
So how to refresh it..on adding or deleting number
Widget build(BuildContext context) {
final controller = Get.put(DataController());
return Scaffold(
body: Column(
children: [
Expanded(
child: Obx(
()=>FutureBuilder(
future: controller.fetchData(),
builder: (context, snapshot) {
const loader = Center(child: CircularProgressIndicator());
if (snapshot.connectionState == ConnectionState.waiting)
return loader;
if (!snapshot.hasData ||
snapshot.data == null ||
snapshot.data!.isEmpty)
return Center(
child: Text('No data found'),
);
if (snapshot.hasError) {
return const Center(
child: Text('Error found'),
);
}
final allData = snapshot.data!;
return ListView.builder(
shrinkWrap: true,
itemCount: allData.length,
itemBuilder: (context, index) {
final data = allData[index];
return ListTile(
title: Text(data.toString()),
);
});
})
),
),
SizedBox(
height: 100,
width: double.infinity,
child: ElevatedButton(onPressed: (){
final num=Random().nextInt(1000);
numbers.add(num);
},child: Text('Add Number'),))
],
),
);
}
data controller
class DataController extends GetxController {
RxBool isLoading = false.obs;
Future<List<int>> fetchData() async {
try {
isLoading.value = true;
final data = numbers;//numbers is a list[int] stored in different file
return data;
} catch (e) {
print('Something wrong');
return [];
} finally {
isLoading.value = false;
}
}
}
for data source
i have just created a list of int in a sepearate file
List<int> numbers=[1,2,3];
2
Answers
To ensure that the FutureBuilder updates when data is added or deleted, you can make use of a StatefulWidget along with a setState call.
by calling "setState" after adding a number, you will trigger a rebuild of that widget, which wil subsequently trigger the FutureBuilder to evaluate again its future, resultin in fetching updated data. your UI will reflect the changes made to the numbers list.
If you are using GetX to manage your state, you should know the 3 options to do that (you can see it here).
Here is how you can change your code to make a dynamic list using Get:
Now this should work.
View:
}
Controller: