【发布时间】:2020-08-10 12:43:51
【问题描述】:
我刚开始学习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: () {},
),
),
),
],
),
),
);
}
}
【问题讨论】:
标签: flutter dart state-management