“‘Widget’ is not a subtype of ‘Widget’ in type cast.”

huangapple go评论69阅读模式
英文:

'Widget' is not a subtype of 'Widget' in type cast

问题

I'm getting the following error during the testing of my frontend app.

>'Widget' is not a subtype of type 'Widget'

Here is the detailed trace shown in the console:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building StreamBuilder<User>(dirty, state:
_StreamBuilderBaseState<User, AsyncSnapshot<User>>#aebd2):
type '({required BuildContext context, required Post post, required String postIdentifier, required
KNPostDisplayContext displayContext, required (Post) => void onPostDeleted}) => Widget' is not a
subtype of type '({BuildContext context, KNPostDisplayContext displayContext, (Post) => void
onPostDeleted, Post post, String postIdentifier}) => Widget' in type cast

<!-- end snippet -->

Here the Widget code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Widget _buildVisibleProfileContent() {
    return Column(
      mainAxisSize: MainAxisSize.max,
      children: [
        Expanded(
          child: KNPostsStream(
              streamIdentifier: 'profile_${widget.user.username}',
              displayContext: _postsDisplayContext,
              prependedItems: _buildProfileContentDetails(),
              controller: _obPostsStreamController,
              postBuilder: _buildPostsStreamPost as KNPostsStreamPostBuilder,
              secondaryRefresher: _refreshUser,
              refresher: _refreshPosts,
              onScrollLoader: _loadMorePosts,
              onPostsRefreshed: _onPostsRefreshed,
              statusIndicatorBuilder: _buildPostsStreamStatusIndicator),
        ),
      ],
    );
  }

<!-- end snippet -->

On debugging, this is the line that is the issue:

> postBuilder: _buildPostsStreamPost as KNPostsStreamPostBuilder,

I upgraded flutter to the latest version: 3.10.1. My pubspec.yaml shows the following:

environment:
  sdk: ">=2.12.0 <3.0.0"

I read other answers. Null safety is the reason why there is an issue but can't figure what needs to be done (maybe my lack of flutter knowledge). Earlier flutter versions didn't throw the error.

Help appreciated.

EDIT: upon request by rohan, I am uploading the post_streams.dart

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

 
final KNPostsStreamPostBuilder? postBuilder;
 
 
List<Widget> _buildStreamPosts() {
  KNPostsStreamPostBuilder postBuilder =
      widget.postBuilder ?? _defaultStreamPostBuilder;
	 

typedef KNPostsStreamPostBuilder = Widget Function(
    {BuildContext context,
    Post post,
    KNPostDisplayContext displayContext,
    String postIdentifier,
    ValueChanged<Post> onPostDeleted});	 

(Note: I have provided the translated code parts as requested. If you have any specific questions or need further assistance, please feel free to ask.)

英文:

I'm getting the following error during the testing of my frontend app.

>'Widget' is not a subtype of type 'Widget'

Here is the detailed trace shown in the console :

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building StreamBuilder<User>(dirty, state:
_StreamBuilderBaseState<User, AsyncSnapshot<User>>#aebd2):
type '({required BuildContext context, required Post post, required String postIdentifier, required
KNPostDisplayContext displayContext, required (Post) => void onPostDeleted}) => Widget' is not a
subtype of type '({BuildContext context, KNPostDisplayContext displayContext, (Post) => void
onPostDeleted, Post post, String postIdentifier}) => Widget' in type cast

<!-- end snippet -->

Here the Widget code :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  Widget _buildVisibleProfileContent() {
    return Column(
      mainAxisSize: MainAxisSize.max,
      children: [
        Expanded(
          child: KNPostsStream(
              streamIdentifier: &#39;profile_${widget.user.username}&#39;,
              displayContext: _postsDisplayContext,
              prependedItems: _buildProfileContentDetails(),
              controller: _obPostsStreamController,
              postBuilder: _buildPostsStreamPost as KNPostsStreamPostBuilder,
              secondaryRefresher: _refreshUser,
              refresher: _refreshPosts,
              onScrollLoader: _loadMorePosts,
              onPostsRefreshed: _onPostsRefreshed,
              statusIndicatorBuilder: _buildPostsStreamStatusIndicator),
        ),
      ],
    );
  }

<!-- end snippet -->

On debugging, this is the line that is the issue :

> postBuilder: _buildPostsStreamPost as KNPostsStreamPostBuilder,

I upgraded flutter to the latest version : 3.10.1. My pubspec.yaml shows the following:

environment:
sdk: ">=2.12.0 <3.0.0"

I read other answers. Null safety is the reason why there is an issue but can't figure what needs to be done(maybe my lack of flutter knowledge). Earlier flutter versions didn't throw the error.

Help appreciated.

EDIT : upon request by rohan, I am uploading the post_streams.dart

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

final KNPostsStreamPostBuilder? postBuilder;
 
 
List&lt;Widget&gt; _buildStreamPosts() {
  KNPostsStreamPostBuilder postBuilder =
      widget.postBuilder ?? _defaultStreamPostBuilder;
	 

typedef KNPostsStreamPostBuilder = Widget Function(
    {BuildContext context,
    Post post,
    KNPostDisplayContext displayContext,
    String postIdentifier,
    ValueChanged&lt;Post&gt; onPostDeleted});	 

<!-- end snippet -->

答案1

得分: 1

所有 KNPostsStreamPostBuilder 的参数都是可选的。然而,_buildPostsStreamPost 的参数是必需的。因此,_buildPostsStreamPost 不能是 KNPostsStreamPostBuilder 的实现。

如果你控制 KNPostsStreamPostBuilder,只需将其参数设为必需。

typedef KNPostsStreamPostBuilder = Widget Function(
    {required BuildContext context,
    required Post post,
    required KNPostDisplayContext displayContext,
    required String postIdentifier,
    required ValueChanged&lt;Post&gt; onPostDeleted});  

在原始应用中,只传入了所有五个参数调用此函数,因此不会影响任何用法。你还可以移除此处的显式转换和其他两个实现中的空值检查。

英文:

All of the parameters of KNPostsStreamPostBuilder are optional. However, the parameters of _buildPostsStreamPost are required. Therefore, _buildPostsStreamPost cannot be an implementation of KNPostsStreamPostBuilder.

If you control KNPostsStreamPostBuilder, simply make its parameters required.

typedef KNPostsStreamPostBuilder = Widget Function(
    {required BuildContext context,
    required Post post,
    required KNPostDisplayContext displayContext,
    required String postIdentifier,
    required ValueChanged&lt;Post&gt; onPostDeleted});  

In the original app, this function is only called with all five parameters supplied, so this won't break any usages. You can also remove the explicit cast here and the null checks in the other two implementations.

huangapple
  • 本文由 发表于 2023年5月25日 22:02:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定