A value of type 'Rx<Future<List<SectionsDBStat>>*>*' can't be assigned to a variable of type

Issue

I am learning Flutter and GetX.
I have got Model:

class SectionsDBStat {

  int notificationsTotalCount;
  int notificationsWaitingForProcessing;
  DateTime notificationsLastArchiveDate;

  SectionsDBStat({
    required this.notificationsTotalCount,
    required this.notificationsWaitingForProcessing,
    required this.notificationsLastArchiveDate
    });

  factory SectionsDBStat.fromJson(Map<String, dynamic> json)  {
      return SectionsDBStat(
        notificationsTotalCount: json['notificationsTotalCount'],
        notificationsWaitingForProcessing: json['notificationsWaitingForProcessing'],
        notificationsLastArchiveDate: DateTime.parse(json['notificationsLastArchiveDate'])
      );

    }  

}

I am trying to create Getx contoller that receive data from http:

class SectionsStatController extends GetxController {
    
    SectionsDBStat sectionsDBStat = Future.value(<SectionsDBStat>).obs;

    getStat() async {
      var value = await http.get('${url}/stat?total');
      if(value.statusCode == 200) {
          Map<String, dynamic> valueMap = jsonDecode(value.body)['data'];
          sectionsDBStat = SectionsDBStat.fromJson(valueMap);
          update();
      } else {
        print("Can't get stat: ${value.statusCode}");
      }

    }

}

I am getting an error:

A value of type 'Rx<Future<List<SectionsDBStat>>*>*' can't be assigned to a variable of type 'SectionsDBStat'.

So I should use another type. But what? I tried final but got error:

'sectionsDBStat' can't be used as a setter because it's final.

Solution

I don’t know why are you using Future, here you are trying to assign Rx<Future<List<SectionsDBStat>>*> to sectionsDBStat and that is not possible

Replace this line

SectionsDBStat sectionsDBStat = Future.value(<SectionsDBStat>).obs;

with this

final sectionsDBStat = SectionsDBStat().obs;

and then assign it like this

sectionsDBStat.value = SectionsDBStat.fromJson(valueMap);

Answered By – Schaban

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.