0

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.

1 Answer 1

1

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);
2
  • thanks for response and welcome to SO! But I am getting error on SectionsDBStat().obs; because it's require data to it's constructor. Feb 26, 2021 at 11:56
  • 1
    try this final sectionsDBStat = Rx<SectionsDBStat>(null);
    – Schaban
    Feb 26, 2021 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.