flutter:同一变量在一个类中返回 null,但在另一个类中工作正常
我有两个不同的类,1)
class PostCard extends StatefulWidget {
final Post? post;
const PostCard({Key? key, this.post}) : super(key: key);
@override
State<PostCard> createState() => _PostCardState();
}
第二类:
class History extends StatefulWidget {
static const id = '/history';
final Post? post;
const History({Key? key, this.post}) : super(key: key);
@override
_HistoryState createState() => _HistoryState();
}
在第一个类中 widget.post!.weight
返回它应该返回的值,但是在历史类中 widget.post!.weight
在“post!”上返回空错误。而且我不明白为什么?谢谢! 这些值都包装在文本小部件中使用:
Text((int.parse(widget.post!.weight)*
(data.durationSeconds ~/
60)).toStringAsFixed(2).padLeft(2, '0')),
错误:
Null check operator used on a null value
When the exception was thrown, this was the stack:
#0 _HistoryState.build.<anonymous closure>
(package:..../..../history.dart:319:56)
#1 SliverChildBuilderDelegate.build
(package:flutter/src/widgets/sliver.dart:457:22)
#2 SliverMultiBoxAdaptorElement._build
(package:flutter/src/widgets/sliver.dart:1215:28)
#3 SliverMultiBoxAdaptorElement.performRebuild.processElement
(package:flutter/src/widgets/sliver.dart:1148:67)
#4 Iterable.forEach (dart:core/iterable.dart:279:35)
#5 SliverMultiBoxAdaptorElement.performRebuild
(package:flutter/src/widgets/sliver.dart:1192:24)
#6 SliverMultiBoxAdaptorElement.update
(package:flutter/src/widgets/sliver.dart:1126:7)
#7 Element.updateChild
(package:flutter/src/widgets/framework.dart:3370:15)
I have two different classes, 1)
class PostCard extends StatefulWidget {
final Post? post;
const PostCard({Key? key, this.post}) : super(key: key);
@override
State<PostCard> createState() => _PostCardState();
}
2nd class:
class History extends StatefulWidget {
static const id = '/history';
final Post? post;
const History({Key? key, this.post}) : super(key: key);
@override
_HistoryState createState() => _HistoryState();
}
In the first one widget.post!.weight
returns the value it supposed to return, however in the History class widget.post!.weight
returns null error on "post!". And I don't understand why? Thanks!
These values are both used wrapped in Text widget:
Text((int.parse(widget.post!.weight)*
(data.durationSeconds ~/
60)).toStringAsFixed(2).padLeft(2, '0')),
error:
Null check operator used on a null value
When the exception was thrown, this was the stack:
#0 _HistoryState.build.<anonymous closure>
(package:..../..../history.dart:319:56)
#1 SliverChildBuilderDelegate.build
(package:flutter/src/widgets/sliver.dart:457:22)
#2 SliverMultiBoxAdaptorElement._build
(package:flutter/src/widgets/sliver.dart:1215:28)
#3 SliverMultiBoxAdaptorElement.performRebuild.processElement
(package:flutter/src/widgets/sliver.dart:1148:67)
#4 Iterable.forEach (dart:core/iterable.dart:279:35)
#5 SliverMultiBoxAdaptorElement.performRebuild
(package:flutter/src/widgets/sliver.dart:1192:24)
#6 SliverMultiBoxAdaptorElement.update
(package:flutter/src/widgets/sliver.dart:1126:7)
#7 Element.updateChild
(package:flutter/src/widgets/framework.dart:3370:15)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论