我正在Flutter中使用GetX包进行状态管理。我试图根据条件是否为真来显示数据。但得到了这个错误,即"type 'bool'不是type cast中type 'RxBool'的子类型“。
下面是我的代码,我试图显示。谢谢帮助。:)
主屏幕
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),
),
);
}
}
显示数据屏幕
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();
},
),
],
);
},
);
}
}
状态控制器文件
import 'package:get/get.dart';
class StatusController extends GetxController {
RxBool isStatusSuccess = false.obs;
}
3条答案
按热度按时间v440hwme1#
只需调用
isStatusSuccess.value
从RxBool中提取bool值00jrzges2#
尝试
isStatusSuccess.isTrue
以获取bool
。50few1ms3#
我们可以用下面的方法。