Flutter GetX can't assign Set<CustomClass> to RxSet<CustomClass>

Issue

I am developing an application and came across a problem, which does not cause an error but pretty frustrating for me.

The situation is that I have an RxSet of CustomClass objects with default initialization to empty Set. When I fetch data from the server, I want to assign the result to this set. As GetX suggests, I should go like myRxSet = dataFetchedFromServer. This gives me a compile time error. myRxSet.value = dataFetchedFromServer still works, but gives an info that I should not assign to .value property.

A sample code can be seen bellow

RxSet<MyCustomClass> myCustomClassEntries = Set<MyCustomClass>().obs;

Future<void> syncData() async {
   myCustomClassEntries = await _fetchDataFromServer(); // this gives me compile time error
   myCustomClassEntries.value = await _ fetchDataFromServer(); // this gives me a warning that RxSet is not intented to be used this way.
}

GetX version: ^4.3.4
Flutter version: 2.2.3
Dart version: 2.13.4

Any idea what I’m doing wrong?

Edit


_fetchDataFromServer() does not matter in this situation, just wanted to give some context to the problem. As simple as it is:

RxSet<MyCustomClass> myCustomClassEntries = Set<MyCustomClass>().obs;

// This line gives me a compile time error. It says Set<MyCustomClass> can't be assigned to RxSet<MyCustomClass>
myCustomClassEntries = new Set<MyCustomClass>();

// This line works, just like everywhere else with GetX, but says .value is no longer intented to be used on RxList, RxSet or RxMap. It recommends to use the syntax above, which gives the error.
myCustomClassEntries.value = new Set<MyCustomClass();

From GetX changelog

Change: You do not need to access the ".value" property of primitives. For Strings you need interpolation. For num, int, double, you will have the normal operators, and use it as dart types. This way, .value can be used exclusively in ModelClasses. Example:

var name = "Jonny" .obs;
// usage:
Text ("$name");

var count = 0.obs;
// usage:
increment() => count ++;
Text("$count");

Thus: List, Map, Set, num, int, double and String, as of this release, will no longer use the .value property.

NOTE: The changes were not break changes, however, you may have missed the details of the documentation, so if you faced the message: "The member ‘value’ can only be used within instance members of subclasses of ‘rx_list.dart’ "you just need to remove the" .value "property from your list, and everything will work as planned. The same goes for Maps and Sets.

Solution

In short, when using a list in Getx, you don’t need to use .value.

...
myCustomClassEntries = Set<MyCustomClass>().obs; // just add .obs and treat it like a regular list. 
final object = MyCustomClass();
myCustomClassEntries.add(object); // no error here and not using .value

I asked for the _fetchDataFromServer() function because it matters in the sense of what you’re trying to return. But regardless, return an observable Set<MyCustomClass>() and then you won’t have type casting errors.

Update after follow up questions:

Just return an RxSet<MyCustomClass> from your _fetchDataFromServer function so you have matching types.

Future<RxSet<MyCustomClass>> _fetchDataFromServer() async {
  final tempList = Set<MyCustomClass>().obs;
// ... your code that populates list from server
  return tempList;
}

You don’t need to clear the list. Below will update myCustomClassEntries with only what is returned from the function.

myCustomClassEntries = await _fetchDataFromServer();

Answered By – Loren.A

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *