Implementing In-App Purchases in Flutter: A Beginner's Guide | amplifyabhi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this particular tutorial I'm  just going to explain you the code   and from there you can make use of  in-app purchases in your application [Music] in this Vlog we are going to see the  implementation of in-app purchase in   our federal application so to implement in-app  purchase we need to upload our application to   the Play Store and from there we can fetch  the products so the very first step I am   going to do is like I'm going to create a new  product I'm going to create a new project here   and I will name it to be like amplify a b and also  I'm going to specify a proper package name here   which I do for all the applications which I upload  to Play Store and here by default kotlin and Swift   are selected for Android and iOS language and  also in the platform section so now let's tap   on finish button here so again this is all the  basic where you create your project and it is   asking us to open a project in the current  directory or in a new window so it is asking   us to open the project in this window or a new  window so let's try to use this window itself   and now we have got a new project here and  now let's get start with the coding part of   in-app purchase here and the very first  thing here we are going to add is like   and it has been available from the flutter dot  official and it's an official flutter package   so now let's try to add the dependency here  and the current version is 3.1.7 and now let's   try to add this dependency to our project  and now let's start with the coding part   and I am going to remove all this basic  coding now and then we will try to add the   stateful widget over here so the very first  thing I am going to add is a stateful widget and now let's try to get the dependencies which we  have added to our file and now try to specify   in-app purchase here and create an object  for it so we are going to get the instance   of in-app purchase and with the help of this  particular in-app purchase object we can make   use of it in our code and the second thing what  we do require is like stream subscription here and this is of a type Dynamic I am  going to specify such that it might   it will accept all the types and let us  try to specify it like stream subscription and here we need to specify it must be initialized  it is saying so let's try to and it's like a late   initialization and later on in the implementation  we will try to make use of it and the third   we required is like product details here so  what are the products available and their   details such that the user may tap on them and  purchase it is specified to be products here   it makes much sense and finally let us specify  a list of products again and here if you go   through our YouTube channel there also you  have like subscription like amplify Abby and   amplifier B pro so and also  there is one more variant but   let's try to confine to these two variants  as we already have products here so we not   specify to be products so we should not specify  to be products again let us try to specify it   to be like variant and now and now let us try  to specify init State here and inside which   let us try to describe let us try to  specify stream subscription equal to   purchase updated so here we need to have the  import like in-app purchase so if you have   not added this particular import so just add  this import so now let us try to specify stream   purchase updated I'm equal to in-app purchase  so here we are able to get the in-app purchase   so let's try to add this so please try to add  this import such that your in-app purchase will   be you are able to use in-app purchase in your  code dot instance Dot purchase stream here okay   so now specify the stream subscription here  and try to make use of this purchase updated   Dot listen so what we are trying to do here is  like we are going to fetch the variants available   from the Google Play and in the next step we  will try to see how the variants are being   added to the Google Play and how we are able  to get them so follow this tutorial till the   end so that you will get a very good Clarity like  how in-app purchases work and for the even let us   try to specify it like purchase list you need to  specify a method stating that listen to purchase and this method we are going to create now and  there we are going to have three scenarios and   we will be based on the Callback we received  then we can execute the code over there so   generally when you make an API call like you  get success and failure call back right so   in in-app purchase also you like if it is  successfully completed or if there is any   error or the payment failed so these will  be the cases which we need to handle again   so now let us try to specify purchase list  as input to this particular method here and we need to create this particular method first   now let us try to specify listen to  purchase here and this will accept list of   purchase details and now purchase details  list dot for each so need to go through each   for each here and then so here the element will be  purchase details and again this will be of a type   asynchronous so that as this runs in  background so it might take some time here   so if purchase details dot status  here we need to find out the status if we if it is double equal to pending  so like purchase status dot pending so   if it is in pending State what needs to be done  here and in else condition if purchase details   so in the else condition also we are having  if else again so purchase details dot status because if it is not in pending state it might  be an error or it is successfully completed so   we need to go through those steps as well here so  if it is error then this particular block of code   will be executed so here you need to specify  double equal to again and else so it doesn't   require to specify if condition again but if there  is any other scenario and to avoid those scenarios   I am going to specify else again here and also  you can just arrange these people's conditions   in much more better way like you need to specif  you can specify uh success over here rather for   other than pending purchase status dot so we  do have all the status here we have already   made use of error pending here and also there are  canceled restored all these options here so now   for successful condition we need to specify  purchase here instead of specifying a separate   else block you can just make it else  if and try to rearrange the code here   so I have arranged the code properly now  so now here let us try to add a scaffold   a snack bar we are going to add here so  scaffold messenger of context so we do   also require context in this particular method  so now let us try to add context as well here   context dot show snack bar and here let us try  to specify snack pass content here so it's like   pending you can just specify here and so let's  specify the content with the help of a text   widget and we specify to be like pending let's add  a constant modifier here and let's add a constant   modifier here and I'm going to add the same  snack bar in every case now and also you can just   add your own code here in this particular block so  of course so what you want to do if the user has   successfully purchased then you can just make sure  you add the coding in this particular blocks here   and now we'll come back to our init State here and  now we have listened to purchase Method as well so   now on done what needs to be do on done so we need  to cancel the subscription which we have specified   so stream subscription dot cancel here we need  to specify once the purchase I mean the process   is done here and also on error scenario so  we need to specify here on error what needs   to be done let's get the error here and within  the block of code you can just again specify a   snack bar here and this time add a message like  error here or like something went wrong so you   can just edit the message accordingly as this is a  tutorial I'm just specifying one-liners over here   and okay we need to also specify context here so  add context so yes this is not a context here so   just in the Android Wear specified  okay so it's build context   make sure you specify this to be a  build context rather than context   so instead of returning a container  let us specify a material app over here   and in the home specifies scaffold now and in  the scaffold let us try to specify an app bar   and within the app bar with the help of a  text widget we need to specify in-app purchase   so this is our heading here so enclose it in  double quotes and the next thing is like body   you need to specify and within the center  tag I am going to specify a text button and this text button will have two parameters like  on press and the next one you need to specify the   title for the button and again with the text  widget we are going to specify this title as   well let's specify it like P and you can add a  constant over here so in the init State itself we   need to fetch the products so I have said right we  need to get the products from the Google Play So   for which let us try to create a method like in it  store here and try to specify this here as well so   here again in its store is of a type asynchronous  here so this is like an asynchronous method again   so in the listen to purchase also we have  specified asynchronous but we have not properly   made use of it so you can just try removing it or  you can also just add some functionality inside   the inside these blocks of code which make use of  the asynchronous which makes use of asynchronous   that is background any background call which  you would like to make after these successful   transactions so in the init store let us try  to specify the product details response here   and so specify a weight here and in-app purchase  so we have specified in-app purchase at the   start of tutorial right so make use of it over  here in-app purchase dot query product details   and here specify the variance so what are  the variance we are having specified over   here so you need to handle the error scenario  as well if product detail response dot error   is double equal to null so if in this  scenario set State make use of set State here   so to make use of such state you need to cut  this particular method and add it inside the   my app here so within this you need to add  this so that we can make use of set State   here so if there is no error here so like  product detail response dot error is null   so you need to store the products now and  for which you need to make the set State oh   so we are away from the stateful widget so  let's copy this code and add it inside the   state so that we can handle set State here so now  make use of these products here and try to store   the data which we get from Google  Play production detail response Dot   product details so we are going to store the  product details here and with this particular   product details we are going to display and  try to perform the purchase in our application   and also let us try to  specify a method like buy here   so here we have added a button right here a  text button in our application so whenever you   tap on these particular buttons this particular  method will be executed so let us specify final purchase param here equal to purchase param so here it is asking  the product details so what are the products   available in our application so we have just now  stored the products right make use of them here   and try to specify 0 here such that  the first product is mean fetch and now make use of in-app purchase Dot pi  consumable and specify the param over here   and and here don't forget to make use of this  particular buy method here and that's what it   is showing here we have not used it anyway so  let's specify it over here and yeah that's it   so now let's try to start our emulator and try to  run this particular code and see the output once so now let's try to run this code and  see the output once in our emulator and here our application is now installed  in the emulator and we can see like a pay   button is there and in-app purchase and  let us try to tap on the pay button here   and we have got an error like range error valid  value range empty 0. so what does this error   States is like our application is not available  on Play Store and also we are not specified as   the application is not available on Play Store  we have not specified any variants or products in   our application so when you try to fetch with the  package name which we have specified then it got 0   here so that is what the error so in the next step  you need to upload this particular application to   your Google Play and I am providing the link in  the description section below like how to create   an application in the Google Play and how you can  upload the application so just again it's a basic   overview tutorial they have shown you all the  steps in detail like how to upload an application   so make use of that particular tutorial and try to  upload your application and if you face any errors   just let me know in the comment section below I  will surely try to help you and for making in-app   purchases according to the new play policies  you need to have a monetization account setup   before getting started with in-app purchase I hope  you can easily create that particular account and   if at all you are having any queries in account  creation just let me know I will try to create   and complete tutorial on Merchant Account creation  and also the most important step is like to upload   application to Play Store you need to have a play  console account for which you will be charged like   25 dollars and the validity is like lifetime if  you are having any queries in account purchase   just let me know I will also try to create a  separate tutorial on it so when you upload this   particular application to the Play Store and  specify your variance in in-app products then   you will try to get those in your application  and you can make an in-app purchase so in this   particular tutorial I'm just going to explain  you the code and from there you can make use   of in-app purchases in your application so this is  for this particular tutorial and if you are having   any queries in this particular tutorial do let me  know in the comment section below and if you like   this tutorial do like share and subscribe to our  channel for more interesting content on flutter
Info
Channel: amplifyabhi coding
Views: 11,856
Rating: undefined out of 5
Keywords:
Id: 0PCJkbOaEpo
Channel Id: undefined
Length: 20min 21sec (1221 seconds)
Published: Mon Jul 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.