【问题标题】:The method '[]' was called on null & receiver: null - Flutter Android在 null 和接收器上调用了方法“[]”:null - Flutter Android
【发布时间】:2020-07-09 07:45:03
【问题描述】:

小部件库捕获了一个异常,不确定是什么问题。这里的 Future builder 似乎有点问题。是不是缺少像 ELSE 子句这样的东西?请指导。代码:

buildProfileHeader() {
    return FutureBuilder(
      future: usersRef.document(widget.profileId).get(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return circularProgress();
        }
        User user = User.fromDocument(snapshot.data);
        return Padding(
          padding: EdgeInsets.all(16.0),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  CircleAvatar(
                    radius: 40.0,
                    backgroundColor: Colors.grey,
                    backgroundImage: CachedNetworkImageProvider(user.photoUrl),
                  ),
                  Expanded(
                    flex: 1,
                    child: Column(
                      children: <Widget>[
                        Row(
                          mainAxisSize: MainAxisSize.max,
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            buildCountColumn("Videos", postCount),
                            buildCountColumn("Followers", 0),
                            buildCountColumn("Following", 0),
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[buildProfileButton()],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 12.0),
                child: Text(
                  user.username,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16.0,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 4.0),
                child: Text(
                  user.displayName,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(top: 2.0),
                child: Text(
                  user.bio,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

调试控制台显示: 在 null 上调用了方法“[]”。 接收方:空 尝试调用: 相关的导致错误的小部件是 未来建造者

尽管如此,buildProfileButton() 也位于 buildProfileHeader() 上方:

buildProfileButton() {
    bool isProfileOwner = currentUserId == widget.profileId;
    if (isProfileOwner) {
      return buildButton(text: "Edit Profile", function: editProfile);
    }
  }

【问题讨论】:

  • 添加 buildProfileButton() 方法等。
  • @VirenVVarasadiya 一切都已完成。唯一的问题在于这个 buildProfileHeader()。特定于未来的建设者。
  • 我认为你可能没有从 buildProfileButton() 方法返回任何东西。我要的是 buildProfileButton() 而不是标头方法。
  • @VirenVVarasadiya 请立即查看。
  • 函数结束时只返回容器。试一试。

标签: flutter dart


【解决方案1】:

从您的 buildProfileButton() 函数中可以看出,如果条件为假,它将不会返回任何内容。所以你必须返回一些东西。

最后返回空容器可能是最好的解决方案。

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2021-08-26
    • 2020-12-06
    • 2021-01-14
    • 2020-05-24
    相关资源
    最近更新 更多