【问题标题】:Flutter GraphQL How to call 2 mutations with one button?Flutter GraphQL 如何一键调用2个突变?
【发布时间】:2021-05-02 06:24:16
【问题描述】:

我创建并测试了 2 个 GraphQL 突变。我在 Flutter 中使用它们,这样当我点击一个按钮时,它会触发两个突变。当我只将其中一个放在 onPressed 中时没关系,只有一个可以工作。那么如何在 onPressed 中同时调用呢?

我尝试创建类似 function1() 和 function2() 来保存每个突变,但 onPressed 似乎没有执行这些函数。

感谢任何帮助。谢谢。

这部分称为第一个将项目附加到列表的突变。我打电话给addToVol(),但它不会触发第二次突变。

Padding(
  padding: EdgeInsets.all(16),
  child: GraphQLProvider(
  client: graphQLConfiguration.client,
  child: CacheProvider(
    child: Mutation(
      options: MutationOptions(
        documentNode: gql(addToSession),
        update: (Cache cache, QueryResult result) {
          return cache;
        },
        onCompleted: (dynamic resultData) {
          print(resultData);
        },
      ),
      builder: (RunMutation runMutation, QueryResult result) {
        if (result.hasException) {
          print(result.exception.toString());
        }
        return FlatButton(
          onPressed: () async {
            if (snapshot.hasData) {
              if (_isAuthenticated) {
                if (!haveSession) {
                  runMutation({
                    'org': activity.org,
                    'sessionId': activity.id,
                    'volId': savedOrgValue,
                  });
//                addToVol();
                } else {

                }
                } else {
                  Navigator.push(
                    context,
                    PageRouteBuilder(
                      pageBuilder: (context, animation1, animation2) => Login(),
                      transitionDuration: Duration(seconds: 0),
                    ),
                  );
                }
              }
            },
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(80.0)),
            padding: EdgeInsets.all(16),
            textColor: Colors.white,
            child: Ink(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(80.0)),
              ),
              child: Container(
                child: Text('Sign Up for Activity'),
              ),
            ),
          );
        }
      )
    )
  ),
),

我在底部有addToVol()

addToVol() {
  GraphQLProvider(
    client: graphQLConfiguration.client,
    child: CacheProvider(
      child: Mutation(
        options: MutationOptions(
          documentNode: gql(registerSessionToVol),
          update: (Cache cache, QueryResult result) {
            return cache;
          },
          onCompleted: (dynamic resultData) {
            print(resultData);
          },
        ),
        builder: (RunMutation runMutation, QueryResult result) {
          if (result.hasException) {
            print(result.exception.toString());
          }
          runMutation({
            'org': activity.org,
            'sessionId': activity.vid,
            'volId': savedOrgValue,
          });
          return;
        }
      )
    )
  );
}

【问题讨论】:

  • 能否也添加与您的问题相关的代码?
  • @AliAlizadeh 我已经添加了部分代码,希望它能澄清谢谢

标签: flutter dart graphql


【解决方案1】:

将每个Mutation 包装到StatefulWidgetState 中,并为每个GlobalKey 添加一个GlobalKey

final mutationKey1 = GlobalKey<MutationState>();

// In the build method
Mutation(
  key: mutationKey1
)

与第二个相同:

final mutationKey2 = GlobalKey<MutationState>();

并在按钮 onPress/onTap 回调中为每个调用 runMutation() 方法:

FlatButton(
  onPressed: (){
    //...
    mutationKey1.currentState.runMutation(mapVariables1);
    mutationKey2.currentState.runMutation(mapVariables2);
    //...
  }
)

请记住,根据您组织代码的方式、访问mutationKey1 和mutationKey2 的方式,访问方式会有所不同。

【讨论】:

  • 谢谢。我创建了 2 个有状态的小部件,按照您的建议,每个突变一个。所有变量都在每个小部件中,但带有“mutationKey1.currentState.runMutation();”的按钮要求位置参数。
  • 在 runMutation(map) 中提供了一个映射,但现在它给出了“NoSuchMethodError The method 'runMutation' was called on null。”
  • 您能展示一下您是如何编写小部件的吗?您的编辑似乎无法为您提供建议。
猜你喜欢
  • 2017-01-21
  • 1970-01-01
  • 2020-12-23
  • 2020-06-17
  • 2020-02-03
  • 2017-05-05
  • 2020-11-07
  • 1970-01-01
  • 2020-01-04
相关资源
最近更新 更多