Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple example calling a mutation on click and handling the response #602

Open
simplenotezy opened this issue May 30, 2024 · 7 comments
Open

Comments

@simplenotezy
Copy link

I am playing around with mutations and would like to have a simple flutter widget that has a button.

When that button is pressed the mutation will be fired, and the button should show a loading state.

It is not clear to me, having been in and out of the documentation and github issues on how to accomplish this.

Can somebody help me in the right direction?

Here me fumbling around in the dark:

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});
  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  GAnalyzeImageReq uploadRequest = GAnalyzeImageReq(
    (b) => b
      ..vars.base64 = '123' // As a hot-fix, I'm setting a dummy value here
      ..executeOnListen = false,
  );

  @override
  initState() {
    super.initState();

    // ideally I wish to listen to the stream here
    // and do something with the data depending if it's an error or not
  }

  @override
  Widget build(BuildContext context) {
    return FerryClientProvider(
      child: (ferryClient) => Expanded(
        child: Align(
          alignment: Alignment.center,
          child: FerryOperation(
            request: uploadRequest,
            builder: (context, response, error) {
              if (response != null && response.data != null) {
                final data = response.data!.analyzeImage;

                if (data != null) {
                  // now I have data, do something with it
                }
              }

              return AnalyzeImageButton(
                isLoading: response?.loading == true,
                onPressed: () async {
                  uploadRequest = uploadRequest.rebuild(
                    (b) => b..vars.base64 = base64Image,
                  );

                  ferryClient.requestController.add(uploadRequest);
                },
              );
            },
          ),
        ),
      ),
    );
  }
}

class FerryClientProvider extends ConsumerWidget {
  final Widget Function(Client client) child;

  const FerryClientProvider({super.key, required this.child});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final ferry = ref.watch(ferryProvider);

    return ferry.when(
      data: child,
      error: (error, stackTrace) {
        return Text('Error: $error');
      },
      loading: () {
        return const CircularProgressIndicator();
      },
    );
  }
}
@simplenotezy
Copy link
Author

@knaeckeKami I know you're probably very busy, but if you ever get a few minutes, I would love your input on this one

@knaeckeKami
Copy link
Collaborator

Did you check https://ferry.gql-dart.dev/docs/flutter-operation-widget#usage-with-mutations ?

If yes, is there anything concrete you have question for?

@simplenotezy
Copy link
Author

Yes thank @knaeckeKami - I did review the whole documentation. Still, from that, it's not quite easy for me to understand how I can fire a single request on submit

@knaeckeKami
Copy link
Collaborator

You can execute final resultFuture = client.request(yourMutationReq).first to execute the request and get a future that resolves when the response is there (or an error happens)

@simplenotezy
Copy link
Author

Interesting, thanks @knaeckeKami - I'll look into it again when working on the project, and report back. Thanks!

@simplenotezy
Copy link
Author

@knaeckeKami If it's awaited, will the loading state also be maintained? E.g. a prop on the button.

@knaeckeKami
Copy link
Collaborator

you'll need to use basic state management approaches (setState, FutureBuilder...) for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants