Flutter:类型“bool”不是类型转换中“RxBool”类型的子类型
Posted
技术标签:
【中文标题】Flutter:类型“bool”不是类型转换中“RxBool”类型的子类型【英文标题】:Flutter: type 'bool' is not a subtype of type 'RxBool' in type cast 【发布时间】:2021-08-02 14:07:05 【问题描述】:我在 Flutter 中使用 GetX 包进行状态管理。我正在尝试根据条件是否为真来显示数据。但是得到这个错误,它说“类型'bool'不是类型转换中'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;
【问题讨论】:
isStatusSuccess
的值在哪里设置?最好包含代码
是的,我已经添加了 HomeScreen 文件。请检查...
这里没有看到任何问题。您应该分享您的完整 StatusController
课程。
【参考方案1】:
只需调用 isStatusSuccess.value
从 RxBool 中提取 bool 值
【讨论】:
【参考方案2】:尝试isStatusSuccess.isTrue
获取bool
。
【讨论】:
以上是关于Flutter:类型“bool”不是类型转换中“RxBool”类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章
未处理的异常:类型“bool”不是类型转换中“String”类型的子类型
当类型转换为 int 时,C/C++ bool 类型是不是总是保证为 0 或 1?
Flutter [错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:类型“int”不是类型转换中“String”类型的子类型
Flutter StreamProvider 'List<dynamic>' 不是类型转换中类型 'List<??>' 的子类型