Flutter: type 'bool' is not a subtype of type 'RxBool' in type cast
Asked Answered
I

3

8

I'm Using GetX Package for State Management in Flutter. I'm trying to show data based on whether condition is true or not. But gets this error which says `type 'bool' is not a subtype of type 'RxBool' in type cast'.

Below is my code which I'm trying to show. Thanks for help. :)

HomeScreen

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:github_users/Controllers/status_controller.dart';
import 'package:github_users/Views/show_data.dart';

class HomeScreen extends StatelessWidget {
  final statusController = Get.put(StatusController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(),
      body: Container(
        width: double.maxFinite,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            successButton(context),
            SizedBox(height: 20),
            faliureButton(context),
          ],
        ),
      ),
    );
  }

//##############################################
//**************  Widget Chunks ***************/

//***************  Appbar ************/
 PreferredSizeWidget appBar() {
    return AppBar(
      backwardsCompatibility: true,
      brightness: Brightness.dark,
      title: Text(
        'Github Users',
      ),
    );
  }

//***************  Success Button ************/
  Widget successButton(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        statusController.fetchSuccessData();
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => ShowData(),
          ),
        );
      },
      child: Text('Fetch Success Data'),
      style: ElevatedButton.styleFrom(
        minimumSize: Size(250, 50),
      ),
    );
  }

//***************  Faliure Button ************/
  Widget faliureButton(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        statusController.fetchFaliureData();
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => ShowData(),
          ),
        );
      },
      child: Text('Fetch Faliure Data'),
      style: ElevatedButton.styleFrom(
        minimumSize: Size(250, 50),
      ),
    );
  }
}

ShowData Screen

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:github_users/Controllers/status_controller.dart';
import 'package:github_users/Models/github_users.dart';
import 'package:github_users/api/users_api.dart';

class ShowData extends StatelessWidget with FaliureStatus {
  final statusController02 = Get.put(StatusController());

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Colors.grey[200],
      appBar: AppBar(
        backwardsCompatibility: true,
        brightness: Brightness.dark,
        title: Text('Show Data'),
      ),
      body: showBody(context),
    );
  }

  showBody(BuildContext context) {
    if (statusController02.isStatusSuccess.value) {
      return FutureBuilder<List<GithubUser>>(
        future: UserApi.getUsersLocally(context),
        builder: (context, snapshot) {
          final users = snapshot.data;
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Center(
                child: CircularProgressIndicator(),
              );
            default:
              if (snapshot.hasError) {
                return buildUsers(users!);
              } else {
                return buildUsers(users!);
              }
          }
        },
      );
    } else {
      return showFaliureDialog(context);
    }
  }

  Widget buildUsers(List<GithubUser> users) => ListView.builder(
        itemCount: users.length,
        itemBuilder: (context, index) {
          final user = users[index];
          return Container(
            height: 130,
            child: Card(
              margin: EdgeInsets.symmetric(
                horizontal: 15,
                vertical: 7,
              ),
              child: Row(
                children: [
                  SizedBox(width: 15),
                  CircleAvatar(
                    backgroundImage: NetworkImage(user.avatarUrl),
                    maxRadius: 30,
                  ),
                  SizedBox(width: 20),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('User ID: ${user.id}'),
                      Text('Type: ${user.type}'),
                      Text('Site Admin: ${user.siteAdmin}'),
                      Text('Username: ${user.login}'),
                    ],
                  ),
                ],
              ),
            ),
          );
        },
      );
}

mixin FaliureStatus {
  showFaliureDialog(BuildContext context) async {
    await Future.delayed(const Duration(seconds: 5), () {});
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Error'),
          content: Text('No data found! Please check your internet connection'),
          actions: [
            TextButton(
              child: Text('Close'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}

StatusController File

import 'package:get/get.dart';

class StatusController extends GetxController {
  RxBool isStatusSuccess = false.obs;
}
Ibarra answered 2/8, 2021 at 14:7 Comment(3)
Where are you setting the value of isStatusSuccess? It would be better to include the codesBottali
Yes I have added the HomeScreen File. Please check...Ibarra
Not seeing any issues here. You should share your full StatusController class.Imaginative
T
15

Just call isStatusSuccess.value to extract bool value from RxBool

Thumbnail answered 7/1, 2022 at 6:26 Comment(0)
F
7

Try isStatusSuccess.isTrue to obtain the bool.

Fillmore answered 7/1, 2022 at 6:8 Comment(0)
L
3

we can use in following way.

if (isStatusSuccess.isTrue)
  isStatusSuccess.value = false;
else
  isStatusSuccess.value = true;
Ladida answered 11/1, 2023 at 9:19 Comment(1)
selected is not a variable that appears in the OP's code. When submitting answers, please try to stick to the question's code instead of changing names.Transferase

© 2022 - 2024 — McMap. All rights reserved.