我刚刚开始学习dart/flutter
,并且非常喜欢flutter框架,但是当我开始使用某种状态管理时遇到了一个问题,因此我选择了Getx
。将state management
集成到项目中之后,我开始遇到这个问题
════════ Exception caught by scheduler library ═════════════════════════════════
The getter 'visible' was called on null.
Receiver: null
Tried calling: visible
════════════════════════════════════════════════════════════════════════════════
Another exception was thrown: NoSuchMethodError: The getter 'visible' was called on null.
,并且已经变得很难调试。如果这里有人可以提供一些解决方法,那就太棒了。
我的模特
class TransactionModel {
List<Data> data;
TransactionModel({
this.data,
});
factory TransactionModel.fromJson(List<dynamic> parsedJson) {
List<Data> transactions = new List<Data>();
transactions = parsedJson.map((i) => Data.fromJson(i)).toList();
return TransactionModel(
data: transactions ?? transactions,
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
String id;
String title;
double amount;
String created;
String fileUrl;
Data({
this.id,
this.title,
this.amount,
this.created,
this.fileUrl,
});
Data.fromJson(Map<String, dynamic> json) {
id = json['id'].toString() ?? json['id'].toString();
title = json['title'] ?? json['title'];
amount = json['amount'] ?? json['amount'];
created = json['created'] ?? json['created'];
fileUrl = json['fileUrl'] ?? json['fileUrl'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id ?? this.id;
data['title'] = this.title ?? this.title;
data['amount'] = this.amount ?? this.amount;
data['created'] = this.created ?? this.created;
data['fileUrl'] = this.fileUrl ?? this.fileUrl;
return data;
}
}
我的控制器
class TransactionController extends GetxController {
var trasanctionListData = List<Data>().obs;
@override
void onInit() {
_getTransactionList();
super.onInit();
}
void _getTransactionList() async {
Future.delayed(
Duration.zero,
() => Get.dialog(
Center(
child: CircularProgressIndicator(),
),
barrierDismissible: false,
),
);
Request request = Request(url: 'transactions', body: null);
request.get().then((value) {
TransactionModel transactionModel = TransactionModel.fromJson(
json.decode(value.body),
);
trasanctionListData.value = transactionModel.data;
}).catchError((onError) {
Get.rawSnackbar(
title: 'Error',
message: '${onError.message}',
icon: Icon(
Icons.error,
color: AppColors.greyishBrown,
),
);
});
}
}
我的观点
class TransactionView extends StatelessWidget {
@override
Widget build(BuildContext context) {
final TransactionController _controller =
Get.put<TransactionController>(TransactionController());
return Scaffold(
backgroundColor: AppColors.cloudyWhite,
appBar: MiAppBar(title: 'Overview'),
body: SingleChildScrollView(
child: Column(
children: [
Carousel(),
Obx(
() => ListView.builder(
itemCount: _controller.trasanctionListData.length,
itemBuilder: (context, index) => TransactionItem(
key: ValueKey(_controller.trasanctionListData[index].id),
title: _controller.trasanctionListData[index].title,
amount: _controller.trasanctionListData[index].amount,
createdAt: _controller.trasanctionListData[index].created,
deleteOnClick: () {},
),
),
),
],
),
),
);
}
}
答案 0 :(得分:0)
在调试并注意错误消息后,我发现我的情况的问题是;那listView在寻找父对象的高度,在我的例子中,父对象是Obx小部件,然后该列和那些小部件的高度没有定义,我通过用容器小部件包装Obx小部件来解决我的问题,并为Container小部件设置高度。我不知道这是否是正确的解决方案,但对于我来说,就是工作。