flutter:同一变量在一个类中返回 null,但在另一个类中工作正常

发布于 2025-01-09 21:43:14 字数 1661 浏览 1 评论 0原文

我有两个不同的类,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文