I am trying to build a simple application with Getx. I am actually trying out the package trying to understand more how it works and I am stuck at a place. Please help me.
There are two screens. 1st screen has two text fields and using the controller I am saving data into a data model and showing it into the ListView builder widget.
I am passing that data using arguments when the user clicks on the individual list tile.
The second screen shows that data in two different Text widgets. I want to decrement the count of one of the Text widgets using the controller. How do I do it?
SCREEN 1 PASSING DATA FROM HERE
Obx(() => ListView.builder(
shrinkWrap: true,
itemCount: controller.tasbees.length,
itemBuilder: (context, index) {
List<Tasbeeh> tasbees = controller.tasbees
.map((value) => Tasbeeh(
tasbeehName: value.tasbeehName,
tasbeehCount: value.tasbeehCount))
.toList();
return Card(
elevation: 3.0,
child: ListTile(
leading: Text(
tasbees[index].tasbeehCount,
style: const TextStyle(
fontSize: Sizes.dimen_18,
fontWeight: FontWeight.w500),
),
title: Text(
tasbees[index].tasbeehName,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: Sizes.dimen_20),
),
trailing: IconButton(
icon: const Icon(Icons.arrow_forward_ios_sharp),
onPressed: () {
var data = {
"name": tasbees[index].tasbeehName,
"count": tasbees[index].tasbeehCount
};
Get.toNamed(Routes.home, arguments: data);
},
),
),
);
},
))
SCREEN 2 RECEIVING DATA HERE.
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have done ${data["name"]} tashbeeh this many times:',
),
vertical30,
//NEED OBX HERE FROM CONTROLLER,
Text('Count: ${data["count"]}'),
vertical50,
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
FloatingActionButton.extended(
onPressed: () {},
tooltip: 'reset',
label: const Text('Reset'),
),
FloatingActionButton.extended(
onPressed: () {
//TODO: UPDATE COUNT HERE WITH DECREMENT METHOD
},
backgroundColor: AppColors.orangeWeb,
tooltip: 'decrement',
label: const Text('Tap Here'),
),
],
),
],
),
),
MY CONTROLLER CLASS where I want my passed arguments from Screen 1 to make it observable and convert String type to int so that I can decrement it, similar to counter app.
class HomeController extends GetxController {
final FirebaseAuthentication _authentication = FirebaseAuthentication();
/*Map<String, String> params = Get.arguments;
String myTest;
*/
var count = Get.arguments["count"];
void decrement() {
//TODO: Decrement Tasbeeh Count here
}
void onLogOut() async {
await _authentication.logOut();
}
}
- How do I make arguments observable? (only 1 argument is needed).
- How to convert that argument into int and decrement it?