如何从flutter从graphQL发送多行字符串?

Yas*_*bey 8 javascript dart graphql flutter

我正在尝试将多行支持放在应用程序的评论部分之一中,但它不接受。

我输入的输入是

Hi
Hello
Hello
Run Code Online (Sandbox Code Playgroud)

它显示这个错误

这是我在将上述输入放入文本字​​段时遇到的错误

这是我为输入字段编写的代码

             ListTile(
                leading: CircleAvatar(
                  backgroundImage: AssetImage(UIData.pkImage),
                ),
                title:  Container(
                  constraints: BoxConstraints(
                    maxHeight: double.infinity,
                    minHeight: 20,
                  ),
                child: TextField(
                  keyboardType: TextInputType.multiline,
                  minLines: 1,//Normal textInputField will be displayed
                  maxLines: 10,// when user presses enter it will adapt to it
                  decoration: InputDecoration(
                      suffix: IconButton(
                        color: Colors.grey,
                        icon: Icon(Icons.send),
                        onPressed: () {
                          createComment();
                        },
                      ),
                      hintText: 'Leave a Comment....',
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(20.0),
                          borderSide: BorderSide(color: Colors.teal))),
                  controller: commentController,
                ),
                ),
              ),
Run Code Online (Sandbox Code Playgroud)

问题在于更新 graphQL 查询并使用 String 块对其进行初始化

String createComments(String postId, var text) {
    return """
mutation{
  createComment(postId: "$postId", 
  data:{
    text: ""$text"",
  }
  ){
    _id
  }
}
"""
;
  }
              
Run Code Online (Sandbox Code Playgroud)

Has*_*ilT 7

我想你正在使用flutter_graphql。使用插值生成变异字符串是不好的做法。您应该使用graphql变量来发送带有突变的数据(并且,发送多行字符串没有问题)。

样本:

String createComments(String postId, var text) {
  const createCommentMutation = """
      mutation createComment(\$postId: String, \$comment:String) { 
        createComment(postId: \$postId, 
          data:{
            text: \$comment,
          }
        ){
          _id
        }
      }
  """;

  dynamic _resp = await _graphClient
          .mutate(MutationOptions(
              document: gql(createCommentMutation),
              variables: {
                'postId': postId, //Add your variables here
                'comment':text
              },
          ));

}
Run Code Online (Sandbox Code Playgroud)

\$postId&的类型\$comment应该与你的 graphql 模式中的类型相同。我已将它们声明为String第一行。

您可以在此处找到相同的文档