Home Blog Page 16

How to use the Value Notifier in flutter.

0
flutter value Notifier

ValueNotifier is a unique subclass of Changenotifier that can hold a single value and alert widgets that are listening to it whenever that value changes. ValueNotifier extends Changenotifier.

ValueNotifier offers an easy and effective way to manage and update state in your application. making it a useful and lightweight tool for developing reactive data sources in Flutter.

When you need to update the UI due to changes in a value. ValueNotifier comes in handy. You can use a ValueNotifier for instance to keep track of changes to the value of a text field in a user interface . so that other UI elements can be updated to reflect the new value.

The main benefit of using a ValueNotifier over state management alternatives like setState or streams is that it is portable and simple to operate. It does not necessitate the creation of intricate streams and stream controllers, nor is it as resource-intensive as setState for straightforward use cases.

What is ValueNotifier?

Let’s first define ValueNotifier so that we can proceed to the specifics of how to use it in Flutter. A straightforward observable data model can be made using the class called ValueNotifier. It is a simplified version of the ChangeNotifier class, which forms the backbone of the reactive programming style used by Flutter. ValueNotifier has a single property named value that stores the model’s current value. The model alerts its listeners whenever its value changes. Due to this, it is simple to develop reactive Flutter applications, in which the UI is updated whenever the data changes.

How can ValueNotifier be used?

It’s very easy to use ValueNotifier in Flutter. You can create a ValueNotifier by providing it with an initial value. For instance , you  follow these steps to create a ValueNotifier that stores an integer value:

// syntax --> ValueNotifier<dataType> myValueNotifier = ValueNotifier<dataType>(value);

// Example :
ValueNotifier<int> myValueNotifier = ValueNotifier<int>(0);

In this example, we making a ValueNotifier that stores the value 0 as an integer.

When you have a use  ValueNotifier you can use the value property to read and modify its value. For instance you can follow these steps to read the value of the ValueNotifier:

Read the value of the ValueNotifier Object:

int currentValue = myValueNotifier.value;

ValueNotifier value property’s value can be modify by assigning  new value . For example the following steps can be used to update the ValueNotifier value to 100 :

myValueNotifier.value = 100;

The ValueNotifier notifier listeners updated whenever value is updated or changed . The addListener method can be used keep track of changes in ValueNotifier variable.

myValueNotifier.addListener(() {
  print('Value changed to ${myValueNotifier.value}');
});

In this example, we’re extending the myValueNotifier with a listener that prints a message whenever its value changes.

To build a basic data model that can be used to update the user interface, ValueNotifier is frequently used in Flutter widgets. ValueNotifier can be used in widgets by simply creating a new instance of it and passing it to the child widgets as necessary. Here’s a good example:

using ValueNotifier


class ValueNotifierExample extends StatelessWidget {
  ValueNotifierExample({Key? key}) : super(key: key);
  final ValueNotifier<int> myValueNotifier = ValueNotifier<int>(42);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(backgroundColor: Colors.blue.shade900,title: const Text("Value notifier implement")),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('Value: ${myValueNotifier.value}'),
          ElevatedButton(
            onPressed: () {
              myValueNotifier.value += 1;
            },
            child: const Text('Increment Value'),
          ),
        ],
      ),
    );
  }
}

Here is an example showing , how to create a ListView in Flutter using the ValueNotifier builder set:

import 'package:flutter/material.dart';

class ListViewValueNotifier extends StatefulWidget {
  const ListViewValueNotifier({super.key});

  @override
  _ListViewValueNotifierState createState() => _ListViewValueNotifierState();
}

class _ListViewValueNotifierState extends State<ListViewValueNotifier> {
  ValueNotifier<int> selectedIndex = ValueNotifier<int>(0);

  List<String> items = [
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.blue.shade900,
        title: const Text("ListView with ValueNotifier"),
      ),
      body: ValueListenableBuilder(
        valueListenable: selectedIndex,
        builder: (BuildContext context, int index, Widget? child) {
          return ListView.builder(
            itemCount: items.length,
            itemBuilder: (BuildContext context, int i) {
              return ListTile(
                title: Text(items[i]),
                selected: i == index,
                onTap: () {
                  selectedIndex.value = i;
                },
              );
            },
          );
        },
      ),
    );
  }
}

Conclusion 👍

ValueNotifier is a component of Flutter’s core Reactive Programming Model, which enables the creation of responsive and effective user interfaces. A ValueNotifier is a simple class that enables the development of reactive data sources that alert listeners when their values change.

ValueNotifier offers an easy and effective way to manage and update state in your application, making it a useful and lightweight tool for developing reactive data sources in Flutter.

Thanks for reading this article 💙…..

Have a beautiful day…..

Create routes in flutter Navigation (Deep-Dive)

0
flutter routes
material page route flutter

An open-source framework called Flutter is used to create powerful, cross-platform mobile apps. The navigation system is one of the most crucial components of any mobile application. Routes a user-friendly and highly customizable navigation system is offered by Flutter. We’ll go into great detail about creating routes in Flutter in this blog .

An application with multiple pages is one in which we design screens with their own widgets one of which is always visible.

The screens are typically displayed in response to user interactions which is accomplished by using navigational functionalities.

What is Navigation?

In a Flutter application switching between screens or views is referred to as “navigation”. Any mobile application must have navigation because it allows users to move between the app’s various screens and interact with its features.

Each screen or view in Flutter is represented by a widget and navigation is accomplished by stacking routes. The previous screen is pushed onto the stack and the new screen is displayed when a user navigates to a new screen.

To implement navigation in an application Flutter offers various patterns and methods including:

Navigator widgets : The Navigator widget in Flutter controls a stack of Route objects that stand in for the app’s screens. It offers ways for developers to push and pop routes from the stack, allowing them to manage the application’s flow.

Named routes: Named routes let app developers give each screen in their application a special name so that users can find it using the Navigator.pushNamed() method.

Bottom navigation bar: A bottom navigation bar is a widget that enables users to quickly switch between the app’s various screens by displaying a list of icons or labels at the bottom of the screen.

Tabs: Users can switch between various categories or views on  screen by using tabs which offer a horizontal list of tab labels.

Drawer:  A drawer is a panel that slides out from the side of the screen and gives the user access to a list of navigation options.

These methods enable programmers to integrate flexible and user-friendly navigation system into their Flutter applications resulting in a seamless user experience.


What are Routes?

Routes in Flutter are a way for users to move between different screens or pages of your application. A widget and distinct route are used to represent each screen in your application. Push and pop operations, which are used to move around a stack, can be used to navigate routes.

The current screen is pushed onto a stack of screens when a user switches between them. The current screen is removed from the stack and the previous screen is shown when the user requests to return to the previous screen.

Making a Simple Route

In Flutter, building a simple route is very easy. Simply defining a widget to represent your screen and link it to a route name is all that is required.

Say we want to move from the home screen to the settings screen. We must create a new widget that represents the settings screen in order to create a route for the settings screen. 

Here’s an illustration:

import 'package:flutter/material.dart';

class SettingsScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
      ),
      body: Center(
        child: Text('Settings Screen'),
      ),
    );
  }
}

Here… for an example is :In the previous illustration, we create a new widget called SettingsScreen that shows a straightforward screen with appBar and text widget in the center.

We must give our widget a route name now that we have it. The MaterialPageRoute class which generates a route that shows a full-screen modal dialog, can be used to achieve this. To create a route for our SettingsScreen, follow these steps:

MaterialPageRoute(
  builder: (context) => SettingsScreen(),
);

In the previous example a fresh MaterialPageRoute is made and connected to our SettingsScreen widget. callback function returns our widget builder parameter.


Choosing a Route to Follow

We can navigate to our route from our home screen now that we have defined it. The Navigator widget, which controls the navigation stack must be used in order to accomplish this.

Using the Navigator.push() method we can add a fresh route to the stack. Here an illustration:

Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => SettingsScreen()),
);

In the aforementioned example we call the Navigator.push() method passing the current BuildContext and our MaterialPageRoute object.

Pop the Route

To go back to the previous screen we can use the Navigator.pop() method. This method returns any data passed to the route along with the top route that was pulled from the navigation stack. Here’s an illustration:

Navigator.pop(context);

In the previous illustration, we used the Navigator.Using the pop() method, send the current BuildContext. This will take you back to the previous screen and pop the top route from the navigation stack

By assigning each route a distinct name, named routing organizes your app’s navigation and makes it simpler to move between screens. Named routing offers a way to navigate to a specific screen by using a named route as opposed to using the widget hierarchy.

The use of named routing in Flutter including how to define named routes pass data between screens, and handle route transitions will be covered in this blog.

Defining Named Routes

To use named routing in Flutter, you first need to define your routes. This is done in the main.dart file of your app, in the MaterialApp widget. The routes parameter, which is a map of String keys and WidgetBuilder values, is where you define your routes.

Let’s say, for illustration purposes, that our app has two screens: a home screen and a details screen. Our named routes can be described as follows:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => FirstScreen(),
        '/second': (context) => SecondScreen(),
      },
    );
  }
}

here defined two routes in the example: “/” for the first screen and “/secondScreen” for the second screen. here, we set the initialRoute to “/,” which is the when app launch on the first screen.

Named routes

We defined here two routes in this  example : “/” for the home screen and 

“/details” for the details screen define . Additionally we set the initial Route to “/” which causes the app launch on the homeScreen.

Making your way to named routes

The Navigator.pushNamed() method which accepts the route name as a parameter can be used to navigate to a named route.

For instance  we could follow these steps to move from the home screen to the details screen:

Navigator.pushNamed(context, '/details'');

By doing this  the user would see the details screen . it would be pushed onto the navigation stack.


Data Transfer between Screens in named routes

You’ll frequently need to pass data between your applocations screens. By passing arguments to the Navigator.pushNamed() method .Let’s say, for illustration that we want to pass the selected item to the details screen from a list of items on the home screen. By giving the item to the Navigator as an argument. we can accomplish this. the pushNamed() function.

Navigator.pushNamed(context, '/details', arguments: item);
class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final item = ModalRoute.of(context)!.settings.arguments as Item;

    return Scaffold(
      appBar: AppBar(
        title: Text(data.title),
      ),
      body: Center(
        child: Text(data.description),
      ),
    );
  }

Managing Route Changes

You can design the way that screens transition within your app in addition to naming routes and getting there. This can be accomplished by specifying a unique transition animation for each route using the MaterialPageRoute function Object() { [native code] }.

Take a fade-in transition, for instance, which we might want to use for the details screen. Making a new MaterialPageRoute and setting its transitionDuration and pageBuilder properties would allow us to accomplish this:

When switching between screens, Flutter offers a variety of built-in transition animations that can be used. There are many of these, such as FadeTransition, SlideTransition, and ScaleTransition. By extending the MaterialPageRoute class and overriding the buildTransitions method, you can also produce your own original transition animation.

class SlideRightRoute extends PageRouteBuilder {
  final Widget page;
  SlideRightRoute({required this.page})
      : super(
          transitionDuration: Duration(milliseconds: 500),
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            var begin = Offset(1.0, 0.0);
            var end = Offset.zero;
            var tween = Tween(begin: begin, end: end);
            var offsetAnimation = animation.drive(tween);
            return SlideTransition(
              position: offsetAnimation,
              child: child,
            );
          },
          pageBuilder: (context, animation, secondaryAnimation) => page,
        );
}

Navigator.push(
  context,
  SlideRightRoute(page: DetailsScreen()),
);

We create a new SlideRightRoute class that extends PageRouteBuilder in the aforementioned example. In order to create a SlideTransition that slides the screen in from the right, we define the transitionsBuilder method and set the transitionDuration property to 500 milliseconds. The Navigator.push method then receives our custom route as a parameter.


Choosing a Different Transition Duration

The duration of the route transition animation can be set using the transitionDuration property. You can change the value from the default of 300 milliseconds to one of your choosing.

For instance, we could modify the transitionDuration property as follows to make our custom slide-in-right transition last for 750 milliseconds:

class SlideRightRoute extends PageRouteBuilder {
  SlideRightRoute({required Widget page})
      : super(
          transitionDuration: Duration(milliseconds: 750),
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            //...
          },
          pageBuilder: (context, animation, secondaryAnimation) => page,
        );
}

defaultRouteName

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Second Screen'),
          onPressed: () {
            Navigator.pushNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go back to Home Screen'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

The HomeScreen widget will be the first screen to be displayed when the programme is opened because the defaultRouteName attribute in this example is set to ‘/’. Each route in the app has a widget attached to it that should appear when the route is browsed to. These routes are defined by the routes parameter.

The Second’ route, which is represented by the SecondScreen widget is reached via the Navigator.pushNamed function. To return to the previous screen which in this instance is the HomeScreen widget use the Navigator.pop function.


PushReplacementNamed 

PushReplacementNamed is a method in Flutter , that lets you switch to a new screen while erasing the one that is currently on the navigation stack.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
        '/third': (context) => ThirdScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Second Screen'),
          onPressed: () {
            Navigator.pushReplacementNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Third Screen'),
          onPressed: () {
            Navigator.pushReplacementNamed(context, '/third');
          },
        ),
      ),
    );
  }
}

class ThirdScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Third Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go back to Home Screen'),
          onPressed: () {
            Navigator.popUntil(context, ModalRoute.withName('/'));
          },
        ),
      ),
    );
  }
}



The HomeScreen and SecondScreen widgets in this example employ the pushReplacementNamed function to direct the user to the second and third routes, respectively. The current screen in the navigation stack gets replaced with the new screen when pushReplacementNamed is invoked.

The popUntil function is used by the ThirdScreen widget to return to the HomeScreen widget. The ModalRoute.withName(‘/’) predicate in this example matches the initial route, and the popUntil function continually pops the current route off the stack until the predicate supplied in its argument is true.


popAndPushNamed  

Pop the current screen off the navigation stack and push a new screen onto the stack all at once with Flutter’s popAndPushNamed function.

Here’s an illustration:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
        '/third': (context) => ThirdScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Second Screen'),
          onPressed: () {
            Navigator.popAndPushNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Third Screen'),
          onPressed: () {
            Navigator.pushNamed(context, '/third');
          },
        ),
      ),
    );
  }
}

class ThirdScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Third Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go back to Home Screen'),
          onPressed: () {
            Navigator.popUntil(context, ModalRoute.withName('/'));
          },
        ),
      ),
    );
  }
}



In this illustration the HomeScreen widget popAndPushNamed function is used to simultaneously pop the active screen off the stack and put the “second” route into the stack. When you wish to replace the current screen with new one while also removing the current screen from the navigation stack this is useful.

When you wish to mimic a “refresh” of the current screen by replacing it with a new instance of the same screen, the popAndPushNamed function might be helpful.

pushNamedAndRemoveUntil

PushNamedAndRemoveUntil is a method in Flutter that lets you advance to a new screen while erasing all previous screens from the stack of screens you’re currently on, up until a certain condition is satisfied.

Here’s an illustration:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
        '/third': (context) => ThirdScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Third Screen and remove previous screens'),
          onPressed: () {
            Navigator.pushNamedAndRemoveUntil(context, '/third', ModalRoute.withName('/'));
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Third Screen'),
          onPressed: () {
            Navigator.pushNamed(context, '/third');
          },
        ),
      ),
    );
  }
}

class ThirdScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Third Screen')),
      body: Center(
        child: RaisedButton(
          child: Text('Go back to Home Screen'),
          onPressed: () {
            Navigator.popUntil(context, ModalRoute.withName('/'));
          },
        ),
      ),
    );
  }
}

The HomeScreen widget in this example uses the pushNamedAndRemoveUntil function to browse to the “third” route and delete all previous screens from the navigation stack. A RoutePredicate is used as the second input to pushNamedAndRemoveUntil, and it defines the prerequisite for halting the removal of earlier routes. ModalRoute.withName(‘/’) in this situation is used to indicate that any routes that come before the ‘/’ route (the first route) should be dropped.

When you wish to start again with new screen after removing a number of screens from the navigation stack . the pushNamedAndRemoveUntil function might be helpful. When creating logout function for instance you could wish to utilize.  it to delete all screens associated with the user’s session and start again with the login screen.

restorablePopAndPushNamed

With the help of the Flutter method restorablePopAndPushNamed you can move to a named route while simultaneously deleting the current route from the navigation stack and saving it for later restoration. Here is an illustration of how to use Flutter’s restorablePopAndPushNamed with named routes:


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/settings': (context) => SettingsScreen(),
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Go to Settings'),
          onPressed: () {
            Navigator.restorablePopAndPushNamed(context, '/settings');
          },
        ),
      ),
    );
  }
}

class SettingsScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Go back to Home'),
          onPressed: () {
            Navigator.restorablePop(context);
          },
        ),
      ),
    );
  }
}

Two arguments are required by restorablePopAndPushNamed: a BuildContext and a String specifying the name of the route to be navigated to. It removes the current route from the navigation stack and replaces it with the named route.

RestorablePop pops the current route off the navigation stack and goes back to the previous screen with just a BuildContext argument.

Conclusion 👍

We thoroughly examined how to create routes in Flutter in this blog post. Routes are a way to move between screens or pages in your application, and we discovered that each screen is represented by a widget and connected to a particular route.

Additionally, we learned how to create a fundamental route, how to use the Navigator.push() method to travel to a route, and how to return.

Thanks for reading this article…..

Have a good day……

Integrate UPI payment gateway in flutter

0
flutter upi
flutter upi payment gateway

You will discover how to incorporate UPI Payment Gateway into your Flutter app in this article.

Payment gateways are now an essential component of mobile applications due to the increase in online transactions. Customers can instantly transfer money between bank accounts in India using the UPI (Unified Payment Interface) payment gateway without any hassle. A great way to give your users a safe and practical payment option is by integrating UPI payment gateway in a Flutter app. We will go over the exact steps for integrating a UPI payment gateway in a Flutter app in this blog post.

Requirements for UPI payments gateway 👍 in flutter

Make sure you have the following prerequisites before we begin:

  • Setup of the Flutter development environment on your computer
  • A tool or emulator to test the application
  • having a functional UPI payment gateway account

Step to Implement UPI Payment in Flutter App

Step 1) : project with the plugin upi_india add this dependency in pubspec.yaml file

upi_india: ^any

Step2) :Run the flutter pub get command to install the plugin after adding the dependency.

Step 3) : import the package named upi_India

We must import the upi_india package in our Dart file after adding the upi_india plugin to the project.

import 'package:upi_india/upi_india.dart';

Step 4) : Initialize UpiIndia

The UpiIndia object needs to be initialized with the necessary configuration information after the upi_india package has been imported. The amount to be transferred, the recipient’s UPI ID, the transaction note, and the payment app the user will use to complete the transaction are all included in the configuration information. An illustration of how to initialize UpiIndia is given below:

UpiIndia upi = UpiIndia();
await upi.initiateTransaction(
  amount: "10.0",
  app: UpiApp.amazonPay,
  receiverName: "Name",
  receiverUpiAddress: "example@upi",
  transactionNote: "Test Transaction",
);

The UpiIndia object is initialized in the code above with a payment of 10 INR to be sent to example@upi. The payment will be made using the Amazon Pay app, and the transaction note is set to “Test Transaction”.

Step 5) : Handle the UPI payment response

The user will be redirected to the UPI payment gateway app to complete the transaction after the payment has been started. The user will be directed back to our app once the transaction has been completed. To ascertain whether the transaction was successful or not, we must manage the UPI payment response. The onTransactionResponse and onTransactionError listeners of the UpiIndia object can be used to handle the payment response. An example of how to handle the payment response is given below:

upi.onTransactionResponse.listen((transactionDetails) {
  print("Transaction done successfully :: ${transactionDetails.toString()}");
});

upi.onTransactionError.listen((error) {
  print("Transaction failed with this error :: ${error.toString()}");
});

In the code above, we are watching for the UpiIndia object’s onTransactionResponse and onTransactionError events. The onTransactionResponse event is triggered if the transaction is successful.

Finally, call the initiateTransaction method when the user clicks on the payment button:

onPressed: () {
    initiateTransaction();
  },
  child: Text("Pay with UPI"),

That’s it! You have successfully integrated the UPI payment gateway in your Flutter app using the upi_india plugin.

For full example code , click here…..

Conclusion

Additionally, the upi_india plugin offers a getAllUpiApps method that can be used to obtain a list of all UPI apps that are currently installed on the user’s device. This method is helpful for showing the user a list of UPI apps and allowing them to select their preferred app for the transaction.

It is more assential part of flutter application to integratr UPI payment gateway in online mode.

The upi_india plugin handles UPI transactions and provides callbacks for transaction success and failure, making it simple to integrate the UPI payment gateway in a Flutter app.

Thanks for reading this article 💙…..

Have a beautiful day….

javascript random number between 1 and 10 or any range

0
js random number between 1 and 10
js random number between 1 and 10

Hi Guys, Welcome to Proto Coders Point, In this short article let’s checkout how to generate a random number between 1 and 10 or between any range you want iun javascript.

In JavaScript, To generate a random number between 1 and 10, we can make use of Math.random() function and by applying a bit of math operation.

Basically ‘Math.random()’ function will generate a random decimal floating number range from 0.0 to 1.0 and thus we need to multiple the generate number by 10 and round it to the nearst integer number by using Math.floor() function.

Here is a example

var rand = Math.random();
console.log(rand);  //0.7520321036592579


let random_number = Math.floor(rand * 10) + 1;
console.log(random_number); // 8

In above code, Math.random() function will generate random number from 0.0 to 1.0 [ 0.7520… is the generated number], then to make it range between 1 – 10 I am multiply it with 10 and add 1 to it because math.floor will round to 1 – 9.


Javascript random number between 1 and 100

Now say you want to generate a number between 1 to 100, so can we acheived by just modifying the scaling and shift value i.e just be multiplying the random number generated by 100 and adding 1 to it . For example:

let randomNumber = Math.floor(Math.random() * 100) + 1;
console.log(randomNumber);

random number between 0 and 1 python

0
python random between 0 and 1
python random between 0 and 1

Hi Guys, Welcome to Proto Coders Point, This short article on “Python random between 0 and 1”.

In Computer Science, Random Number Generation is key concept. While building any kind of application there is a need somewhere of random number between 0 and 1 python, might be for simulate events, encrypt data, statistical analysis.

In Python to generate a random number between 0 and 1, we can make use of python ‘random’ module

random in python

The `random` module in python is a built-in library that provide a functionality like generating random number. To generate a random number between 0 and 1, we can use random() function, which will return a decimal point number range from 0.0 to 1.0.

random number between 0 and 1 python example

import random

random_number = random.random()
print(random_number) // 0.8046595183024017

As you can see, In above code I have firstly imported random module then used to generate a random number from 0.0 to 1.0 and thus the random number is printed in console using print().

Flutter random word generator, nouns, verbs, names, sentences

0
Flutter Random Word Generator
Flutter Random Word Generator

Hi Guys, Welcome to Proto Coders Point, In this Flutter Article let’s checkout how to generate a random word in flutter dart. first by picking a randon word from a given list and second exampe by using a word_generator flutter library.

Generate random nouns example in flutter dart

Below is an dart program example to generate random nouns:

import 'dart:math';

void main() {
  List<String> nouns = [
    'Balloon',
    'Car',
    'Horse',
    'Raincoat',
    'Train',
    'Monkey',
    'Window',
    'Rocket',
    'Iron',
    'Girl'
  ];
  
  Random random = Random();
  int randomIndex = random.nextInt(nouns.length);
  String randomNoun = nouns[randomIndex];
  
  print(randomNoun);
}

In above Program we have a list of noun words created, Then we make use of Random class from math package that will generate a random number from 0 to the length of the list, and thus us use this random number and retrieve a noun list index and print it on console.


Flutter dart program to generate a random word

In above program we saw that we created a list of word and randomly pick a word from the list, but suppose you want to generate a random word without predefined list of words.

Here is one way by by which we can generate random nouns or words without using pre-defined words list is by using flutter word generator library.

First add the dependency in your flutter project

open pubspec.yaml file and under dependencies section add:

dependencies:
  word_generator:

Generate Random Nouns in flutter dart

import 'package:word_generator/word_generator.dart';

void main() {
  String randomNoun = WordGenerator().randomNoun()
  print(randomNoun);
}

Generate Random Verbs in flutter dart

 void main() {
        String randomNoun = WordGenerator().randomVerb()
        print(randomNoun);
 }

Generate Random Sentence using word generator

void main() {
        String randomNoun = WordGenerator().randomSentence();
        print(randomNoun);
 }

Generate Random Names in flutter dart

void main() {
        String randomNoun = WordGenerator().randomName()
        print(randomNoun);
}

Generate Random usernames in dart

The below Code will generate a random name using word generator package, We need to make the username in such a ways that it dont contain space and should be in lowercase.

String randomNoun = WordGenerator().randomName();
String username = randomNoun.toLowerCase().replaceAll(' ', '');
print(username);