小部件库捕获的异常 ═ 方法 '+' 在 null 上调用。接收器:空 尝试呼叫:+( " " )



大家好,我是新来的扑动
当我试图从我的小部件呈现评论列表时,我被这个错误所困扰。


widgets库捕获的异常

方法'+'被调用为null。接收器:零试打:+(">
相关的导致错误的小部件是注释项
lib…Commentsmain。

我的部件代码

class CommentItem extends StatefulWidget {
CommentItem({Key key, this.comment}) : super(key: key);
final CommentModel comment;
@override
_CommentItemState createState() => _CommentItemState();
}
class _CommentItemState extends State<CommentItem> {
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 20),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
widget.comment.user.picture != null
? avatar(url: widget.comment.user.picture)
: const SizedBox.shrink(),
commentDetails()
],
),
);
}
Widget commentDetails() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
userName(
widget.comment.user.name + ' ' + widget.comment.user.surname), // error line
SizedBox(
width: 10,
),
commentTimeStap(widget.comment.createdAt)
],
),
SizedBox(
height: 10,
),
widget.comment.type == 'text'
? textCommentContent(widget.comment.body)
: imageCommentContent(widget.comment.body),
SizedBox(
height: 10,
),
commentEdit(),
],
);
}
Widget userName(String name) {
return Text(
name,
style: userNameStyle(),
);
}
Widget commentTimeStap(String time) {
return Text(Helpers.timeAgoSinceDate(time));
}
Widget textCommentContent(String body) {
return Text(
body,
style: commentContentStyle(),
);
}
Widget imageCommentContent(String body) {
return Image.network(
Api.assetsurl + '/$body',
fit: BoxFit.cover,
width: MediaQuery.of(context).size.width - 100,
height: 200,
);
}
Widget commentEdit() {
return Row(
children: <Widget>[
widget.comment.type == 'text'
? commentButton('Edit', onClick: null)
: Container(),
SizedBox(
width: 10,
),
Text('.'),
SizedBox(
width: 10,
),
commentButton('Delete', onClick: () async {
await deleteModal();
}),
],
);
}
Widget commentButton(String text, {@required Function onClick}) {
return GestureDetector(
onTap: onClick,
child: Text(
text,
style: commentButtonStyle(),
));
}
Future<void> deleteModal() async {
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Are You Sure ?'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Once You Delete , It's Gone For Good.'),
],
),
),
actions: <Widget>[
FlatButton(
onPressed: () async {
Future.delayed(Duration(milliseconds: 5000)).then((value) {
Navigator.of(context).pop();
});
},
child: Text('Delete Comment')),
FlatButton(
onPressed: () async {
Future.delayed(Duration(milliseconds: 5000)).then((value) {
Navigator.of(context).pop();
});
},
child: Text('Cancel'))
],
);
});
}
}

我的评论模型

import 'package:gn_plumbing/Core/models/user_model.dart';
class CommentModel {
int id;
String userId;
String body;
String type;
String projectId;
String createdAt;
UserModel user;
CommentModel(
{this.id,
this.userId,
this.body,
this.projectId,
this.type,
this.createdAt,
this.user});
factory CommentModel.fromJson(Map<String, dynamic> json) {
//* initialize comment creator
var _user = new UserModel();
if (json != null && json.containsKey("staff")) {
_user = UserModel.fromJson(json['staff']);
}
return CommentModel(
id: int.parse(json["id"].toString()) ?? 0,
userId: json["user_id"] ?? "",
body: json["body"] ?? "",
type: json["type"] ?? "",
createdAt: json["created_at"] ?? "",
projectId: json["project_id"] ?? "",
user: _user);
}
}

Json示例

"comments": [
{
"id": 2,
"user": {
"id": 3,
"name": "Lucky",
"surname": "Motseki",
"id_number": 123456789654,
"picture": "1629103951.jpg",
"medical": "5362",
"rate": "4",
"contract": "1998-11-25",
"email": "lucky.m@dotcom.africa"
},
"body": "test",
"type": "text",
"created_at": "2021-02-05T12:00:25.000000Z"
},
]

用户模型

class UserModel {
final int id;
final String name;
final String surname;
final String picture;
final String email;
final String medical;
final String contract;
UserModel({
this.id,
this.name,
this.surname,
this.picture,
this.email,
this.medical,
this.contract,
});
factory UserModel.fromJson(Map<String, dynamic> json) {
return UserModel(
id: json['id'],
name: json["name"],
surname: json["surname"],
picture: json["picture"],
email: json["email"],
medical: json["medical"],
contract: json["contract"],
);
}
} // user class
class ApiTokenModel {
String apiToken;
ApiTokenModel({this.apiToken});
factory ApiTokenModel.fromJson(Map<String, dynamic> json) {
return ApiTokenModel(apiToken: json["authToken"] ?? "");
}
}

提前感谢大家。

我怀疑你有空类型调用和操作时,他们不应该。这种方法可以通过将项目迁移到null安全来轻松解决。

到目前为止,您可以在

行中检查null:
if(widget.comment.user != null && widget.comment.user.name != null)
userName(widget.comment.user.name + ' ' + widget.comment.user.surname),

相关内容

最新更新