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;
}
isStatusSuccess
? It would be better to include the codes – BottaliStatusController
class. – Imaginative