More
Flutter, Google Wallet, and Back4App: Environmental Rewards App Tutorial
15 min
introduction in this tutorial, we'll create a flutter app that encourages users to take environmental actions, rewarding them with digital badges and event tickets stored in google wallet we’ll integrate back4app as the backend to manage user data and track environmental contributions each time a user completes a task, they receive a collectible badge or event ticket, which can be added to their google wallet for safekeeping features users can log environmental tasks (e g , recycling, volunteering) users earn digital badges and event passes based on their contributions google wallet stores and displays these digital assets backend integration with back4app for tracking and data management prerequisites flutter development environment follow the flutter installation guide https //flutter dev/docs/get started/install back4app account sign up at back4app https //www back4app com google wallet api access set up your project by following the google wallet api documentation https //developers google com/wallet google wallet api keys generate and use the required api keys for accessing google wallet step 1 setting up back4app as backend 1 1 create a back4app project sign in to back4app https //www back4app com/ and create a new project create a parse class called environmentalactions with the following fields username (string) the username of the user actiontype (string) type of environmental action (e g , recycling, tree planting) timestamp (datetime) the date when the user completed the action rewardissued (boolean) whether a reward has been issued for the action 1 2 add sample data to back4app you can add some sample records for testing purposes, but the app will handle data entry later when users complete tasks step 2 initialize parse sdk in flutter 2 1 create a new flutter project open your terminal and create a new flutter project flutter create env rewards app 2 2 add dependencies open the pubspec yaml file and add the required dependencies dependencies flutter sdk flutter parse server sdk flutter latest version http ^0 13 3 # for networking google wallet pass latest version # example package to handle google wallet passes run flutter pub get to install the packages 2 3 initialize parse sdk in main file in lib/main dart , initialize the parse sdk by adding your back4app credentials import 'package\ flutter/material dart'; import 'package\ parse server sdk flutter/parse server sdk flutter dart'; void main() async { widgetsflutterbinding ensureinitialized(); const keyapplicationid = 'your back4app app id'; const keyclientkey = 'your back4app client key'; const keyparseserverurl = 'https //parseapi back4app com'; await parse() initialize(keyapplicationid, keyparseserverurl, clientkey keyclientkey, autosendsessionid true); runapp(myapp()); } class myapp extends statelesswidget { @override widget build(buildcontext context) { return materialapp( title 'environmental rewards', home environmentalrewardsscreen(), ); } } replace your back4app app id and your back4app client key with your actual back4app credentials step 3 build the ui for logging environmental actions we'll create a basic ui that allows users to log their environmental actions and view their digital rewards 3 1 create the environmentalrewardsscreen widget create a new widget in lib/environmental rewards screen dart import 'package\ flutter/material dart'; import 'package\ parse server sdk flutter/parse server sdk flutter dart'; import 'package\ intl/intl dart'; // for formatting dates class environmentalrewardsscreen extends statefulwidget { @override environmentalrewardsscreenstate createstate() => environmentalrewardsscreenstate(); } class environmentalrewardsscreenstate extends state\<environmentalrewardsscreen> { final texteditingcontroller usernamecontroller = texteditingcontroller(); string? selectedaction; bool loading = false; string? rewardmessage; final list\<string> actions = \['recycling', 'tree planting', 'volunteering']; future\<void> logaction() async { if ( usernamecontroller text isempty || selectedaction == null) { scaffoldmessenger of(context) showsnackbar(snackbar( content text('please enter a username and select an action '), )); return; } setstate(() { loading = true; }); // save action to back4app final actionobject = parseobject('environmentalactions') set('username', usernamecontroller text) set('actiontype', selectedaction) set('timestamp', datetime now()) set('rewardissued', false); final response = await actionobject save(); if (response success) { setstate(() { rewardmessage = 'action logged! check your rewards!'; }); // call google wallet api to issue a reward here await issuereward(); } else { setstate(() { rewardmessage = 'failed to log action '; }); } setstate(() { loading = false; }); } future\<void> issuereward() async { // this is where you'd integrate the google wallet api to issue a digital badge // use the google wallet sdk to generate a pass and add it to the user's wallet } @override widget build(buildcontext context) { return scaffold( appbar appbar(title text('environmental rewards')), body padding( padding const edgeinsets all(16 0), child column( children \[ textfield( controller usernamecontroller, decoration inputdecoration(labeltext 'username'), ), sizedbox(height 16), dropdownbutton\<string>( value selectedaction, hint text('select an environmental action'), items actions map((string action) { return dropdownmenuitem\<string>( value action, child text(action), ); }) tolist(), onchanged (string? newvalue) { setstate(() { selectedaction = newvalue!; }); }, ), sizedbox(height 16), elevatedbutton( onpressed loading ? null logaction, child loading ? circularprogressindicator() text('log action'), ), sizedbox(height 16), if ( rewardmessage != null) text( rewardmessage!, style textstyle(color colors green)), ], ), ), ); } } this ui allows the user to enter their username select an environmental action log the action, which triggers the backend and issues a reward using the google wallet api step 4 integrating google wallet for issuing rewards 4 1 set up google wallet api follow the google wallet api documentation https //developers google com/wallet to set up the api and obtain your api keys 4 2 issue a digital badge with google wallet in the issuereward() method, you will implement the logic to create a digital badge (generic pass) and save it to the user's google wallet here's a basic structure future\<void> issuereward() async { final badgedata = { 'title' 'environmental hero', 'description' 'awarded for completing environmental actions ', // add any additional information or metadata }; // call google wallet api to generate a pass // you can use the 'google wallet pass' package or direct api calls final response = await creategooglewalletpass(badgedata); // placeholder for actual implementation if (response success) { scaffoldmessenger of(context) showsnackbar(snackbar( content text('badge issued! check your google wallet '), )); } else { scaffoldmessenger of(context) showsnackbar(snackbar( content text('failed to issue badge '), )); } } step 5 running the app run the app using flutter run to start logging actions and receiving rewards when a user logs an action, it will be saved to back4app, and a badge will be issued using the google wallet api conclusion this tutorial showed how to build an environmental rewards app using flutter , google wallet , and back4app users log environmental actions, and upon completion, they receive digital badges or event passes stored in their google wallet this project could be expanded with social sharing features, leaderboards, or even location based rewards using qr codes for more information, refer to flutter documentation https //flutter dev/docs google wallet api documentation https //developers google com/wallet back4app documentation https //www back4app com/docs