0

我正在尝试使用 graphql_flutter 的订阅小部件,但我的应用无法编译。我不断收到一个空安全错误:

错误:参数类型'Widget Function({dynamic error, bool loading, dynamic payload})'不能分配给参数类型'Widget Function({dynamic error, bool loading, dynamic payload})',因为'Widget Function ({dynamic error, bool loading, dynamic payload})' 可以为空,而 'Widget Function({dynamic error, bool loading, dynamic payload})' 不是。

  • “小部件”来自“package:flutter/src/widgets/framework.dart”(“/C:/flutter/packages/flutter/lib/src/widgets/framework.dart”)。? 订阅(getMessages,“”,构建器:({

这是我的代码:

return Scaffold(
        backgroundColor: Colors.grey[200],
        appBar: AppBar(
          title: const Text("Chats"),
        ),
        resizeToAvoidBottomInset: true,
        body: GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: Column(children: [
                  Subscription(getMessages, "", builder: ({
                      required bool loading,
                      dynamic payload,
                      dynamic error,
                    }) {
                      return error
                          ? Text(error)
                          : loading
                              ? const CircularProgressIndicator()
                              : payload['messages'].map((message) {
                                  return Row(
                                      mainAxisAlignment:
                                          firstName == message.user
                                              ? MainAxisAlignment.end
                                              : MainAxisAlignment.start,
                                      children: <Widget>[
                                        firstName == message.user
                                            ? Container()
                                            : Expanded(
                                                flex: 1,
                                                child: Container(
                                                    margin: const EdgeInsets
                                                        .fromLTRB(0, 0, 15, 0),
                                                    padding: const EdgeInsets
                                                        .fromLTRB(0, 5, 0, 0),
                                                    width: 50.0,
                                                    height: 50.0,
                                                    decoration: BoxDecoration(
                                                        borderRadius:
                                                            BorderRadius
                                                                .circular(25),
                                                        border: Border.all(
                                                            width: 3,
                                                            color:
                                                                Colors.black)),
                                                    child: Text(
                                                      firstName
                                                          .substring(0, 2)
                                                          .toUpperCase(),
                                                      style: const TextStyle(
                                                          fontSize: 18),
                                                    ))),
                                        Expanded(
                                            flex: 6,
                                            child: Container(
                                                padding:
                                                    const EdgeInsets.all(10.0),
                                                decoration: BoxDecoration(
                                                  borderRadius:
                                                      BorderRadius.circular(10),
                                                ),
                                                color: firstName == message.user
                                                    ? Colors.green[200]
                                                    : Colors.grey[500],
                                                child: Text(
                                                  message.content,
                                                  style: TextStyle(
                                                      fontSize: 25,
                                                      color: firstName ==
                                                              message.user
                                                          ? Colors.white
                                                          : Colors.black),
                                                )))
                                      ]);
                                });
                    })

我在网上搜索过,但找不到任何有这个确切错误的帖子。我的猜测是我在 Subscription 小部件中编写的代码是可以为空的,而 Subscription 小部件本身(来自包)不是。我只是不确定如何调和两者。

我希望我发布的代码不会令人困惑。错误来自订阅小部件。我在Github上也有代码。

4

0 回答 0