Hello
Can someone please tell me how can i observe a boolean, i tried this code
bool_showOnlyFavorites = false.obs
but then i get this message:
A value of type 'RxBool' can't be assigned to a variable of type 'bool'.
Try changing the type of the variable, or casting the right-hand type to 'bool'.
i'm new to this so maybe i'm missing something
Thank you.
The bool is a class of Flutter, it is not reactive.
To make your bool an observable, you must either make it final, or type it as RxBool.
RxBool _showOnlyFavorites = false.obs
or
final _showOnlyFavorites = false.obs
// var _showOnlyFavorites = false.obs this too works
Yeah but when i tried to change it to RxBool, and change the value to true through a button on pressed.
_showOnlyFavorites = true
I get a message saying:
A value of type bool can't be assigned to a variable of type Rxbool
Also i want to be able check the bool status using '?': to apply a certain method based on the boolean status.
_showOnlyFavorites.value = true
to made a set, you need access ".value"
I'm sorry, i still get this error message:
type 'RxBool' is not a subtype of type 'bool'
even after using '.value', i think this happens when i check the status with '?:'
Please insert a sample code where the problem occurs for reproduction.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import './widgets/products_grid.dart';
import '../../../controller/products/products.dart';
enum FilterOptions { Favorites, All }
RxBool _showOnlyFavorites = false.obs;
class ProductsOverviewScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text(
'MyShop',
),
actions: [
PopupMenuButton(
icon: Icon(Icons.more_vert),
onSelected: (FilterOptions selected) {
if (selected == FilterOptions.Favorites) {
_showOnlyFavorites.value = true;
} else {
_showOnlyFavorites.value = false;
}
},
itemBuilder: (_) => [
PopupMenuItem(
child: Text('Only Favorites'),
value: FilterOptions.Favorites,
),
PopupMenuItem(
child: Text('Show All'),
value: FilterOptions.All,
)
]),
],
),
body: ProductsGrid(showFavs: _showOnlyFavorites),
),
);
}
}`
____________________________
`class ProductsGrid extends StatelessWidget {
final showFavs;
ProductsGrid({this.showFavs});
@override
Widget build(BuildContext context) {
return GetBuilder<Products>(
init: Products(),
builder: (data) => GridView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: data.items.length,
itemBuilder: (ctx, index) => ProductItem(
pIndex:
showFavs ? data.favoriteItems[index] : data.items[index],
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
)),
);
}
}
I'm afraid if i post more code the thread will be to long.
you can change this line if you want for fast troubleshooting
showFavs ? data.favoriteItems[index] : data.items[index],
please forgive my poor code as i'm still learning.
thank you.
OK this my project if my code didn't make any sense.
Alright, fixed my code, but the question remains.
how can i observe the boolean status to avoid using StatefulWidget.
Thank you.
getx_pattern_example.zip
You need to wrap the widget you want to update within an Obx()
You need to wrap the widget you want to update within an Obx()
My code runs fine now, i used .value and i can listen to the boolean value.
I still need to use statufulwidget in the "products_overview.dart" to update the screen.so my final question, is there anyway i can avoid this using Getx?
Thank you for taking the time to answer my questions.
Change Getbuilder to Getx.
Change your list to yourList.obs
Or use Update
Thank you for your time, I'll continue this on discord.
Most helpful comment
You need to wrap the widget you want to update within an Obx()