Architecture Application

1. Tutorial

In this chapter, let us discuss the architecture of the Flutter framework. Widgets The core concept of the Flutter framework is In Flutter, Everything is a widget. Widgets are basically user interface components used to create the user interface of the application. In Flutter, the application is itself a widget. The application is the top- level widget and its UI is build using one or more children (widgets), which again build using its children widgets. This composability feature helps us to create a user interface of any complexity. For example, the widget hierarchy of the hello world application (created in previous chapter) is as specified in the following diagram − Here the following points are worth notable − Gestures Flutter widgets support interaction through a special widget, GestureDetector. GestureDetector is an invisible widget having the ability to capture user interactions such as tapping, dragging, etc., of its child widget. Many native widgets of Flutter support interaction through the use of GestureDetector. We can also incorporate interactive feature into the existing widget by composing it with the GestureDetector widget. We will learn the gestures separately in the upcoming chapters. Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career. Concept of State Flutter widgets support State maintenance by providing a special widget, StatefulWidget. Widget needs to be derived from StatefulWidget widget to support state maintenance and all other widget should be derived from StatefulWidget. Flutter widgets are reactive in native. This is similar to reactjs and StatefulWidget will be auto re- rendered whenever its internal state is changed. The re-rendering is optimized by finding the difference between old and new widget UI and rendering only the necessary changes Layers The most important concept of Flutter framework is that the framework is grouped into multiple category in terms of complexity and clearly arranged in layers of decreasing complexity. A layer is build using its immediate next level layer. The top most layer is widget specific to Android and iOS. The next layer has all flutter native widgets. The next layer is Rendering layer, which is low level renderer component and renders everything in the flutter app. Layers goes down to core platform specific code The general overview of a layer in Flutter is specified in the below diagram − The following points summarize the architecture of Flutter − We will discuss all these concepts in detail in the upcoming chapters.

October 12, 2024 / 0 Comments
read more

Creating Simple Application in Android Studio

1. Tutorial

In this chapter, let us create a simple Flutter application to understand the basics of creating a flutter application in the Android Studio. Step 1 − Open Android Studio Step 2 − Create Flutter Project. For this, click File → New → New Flutter Project Step 3 − Select Flutter Application. For this, select Flutter Application and click Next. Step 4 − Configure the application as below and click Next. Step 5 − Configure Project. Set the company domain as flutterapp.tutorialspoint.com and click Finish. Step 6 − Enter Company domain. Android Studio creates a fully working flutter application with minimal functionality. Let us check the structure of the application and then, change the code to do our task. The structure of the application and its purpose is as follows − Various components of the structure of the application are explained here − Step 7 − Replace the dart code in the lib/main.dart file with the below code − Let us understand the dart code line by line. Step 8 − Now, run the application using, Run → Run main.dart Step 9 − Finally, the output of the application is as follows −

October 12, 2024 / 0 Comments
read more

Installation

1. Tutorial

This chapter will guide you through the installation of Flutter on your local computer in detail. Installation in Windows In this section, let us see how to install Flutter SDK and its requirement in a windows system. Step 1 − Go to URL, https://flutter.dev/docs/get-started/install/windows and download the latest Flutter SDK. As of April 2019, the version is 1.2.1 and the file is flutter_windows_v1.2.1-stable.zip. Step 2 − Unzip the zip archive in a folder, say C:\flutter\ Step 3 − Update the system path to include flutter bin directory. Step 4 − Flutter provides a tool, flutter doctor to check that all the requirement of flutter development is met. Step 5 − Running the above command will analyze the system and show its report as shown below − The report says that all development tools are available but the device is not connected. We can fix this by connecting an android device through USB or starting an android emulator. Step 6 − Install the latest Android SDK, if reported by flutter doctor Step 7 − Install the latest Android Studio, if reported by flutter doctor Step 8 − Start an android emulator or connect a real android device to the system. Step 9 − Install Flutter and Dart plugin for Android Studio. It provides startup template to create new Flutter application, an option to run and debug Flutter application in the Android studio itself, etc., Installation in MacOS To install Flutter on MacOS, you will have to follow the following steps − Step 1 − Go to URL, https://flutter.dev/docs/get-started/install/macos and download latest Flutter SDK. As of April 2019, the version is 1.2.1 and the file is flutter_macos_v1.2.1- stable.zip. Step 2 − Unzip the zip archive in a folder, say /path/to/flutter Step 3 − Update the system path to include flutter bin directory (in ~/.bashrc file). Step 4 − Enable the updated path in the current session using below command and then verify it as well. Flutter provides a tool, flutter doctor to check that all the requirement of flutter development is met. It is similar to the Windows counterpart. Step 5 − Install latest XCode, if reported by flutter doctor Step 6 − Install latest Android SDK, if reported by flutter doctor Step 7 − Install latest Android Studio, if reported by flutter doctor Step 8 − Start an android emulator or connect a real android device to the system to develop android application. Step 9 − Open iOS simulator or connect a real iPhone device to the system to develop iOS application. Step 10 − Install Flutter and Dart plugin for Android Studio. It provides the startup template to create a new Flutter application, option to run and debug Flutter application in the Android studio itself, etc.,

October 12, 2024 / 0 Comments
read more

Introduction

1. Tutorial

In general, developing a mobile application is a complex and challenging task. There are many frameworks available to develop a mobile application. Android provides a native framework based on Java language and iOS provides a native framework based on Objective-C / Swift language. However, to develop an application supporting both the OSs, we need to code in two different languages using two different frameworks. To help overcome this complexity, there exists mobile frameworks supporting both OS. These frameworks range from simple HTML based hybrid mobile application framework (which uses HTML for User Interface and JavaScript for application logic) to complex language specific framework (which do the heavy lifting of converting code to native code). Irrespective of their simplicity or complexity, these frameworks always have many disadvantages, one of the main drawback being their slow performance. In this scenario, Flutter – a simple and high performance framework based on Dart language, provides high performance by rendering the UI directly in the operating system’s canvas rather than through native framework. Flutter also offers many ready to use widgets (UI) to create a modern application. These widgets are optimized for mobile environment and designing the application using widgets is as simple as designing HTML. To be specific, Flutter application is itself a widget. Flutter widgets also supports animations and gestures. The application logic is based on reactive programming. Widget may optionally have a state. By changing the state of the widget, Flutter will automatically (reactive programming) compare the widget’s state (old and new) and render the widget with only the necessary changes instead of re-rendering the whole widget. We shall discuss the complete architecture in the coming chapters. Features of Flutter Flutter framework offers the following features to developers − Advantages of Flutter Flutter comes with beautiful and customizable widgets for high performance and outstanding mobile application. It fulfills all the custom needs and requirements. Besides these, Flutter offers many more advantages as mentioned below − Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career. Disadvantages of Flutter Despite its many advantages, flutter has the following drawbacks in it −

October 12, 2024 / 0 Comments
read more

HTML DOM

6. Advance Topics

Every webpage can be considered as object and it exists inside a browser window. We can access the webpage using the web browser and it needed connected to the internet. The DOM is the acronyms of Document object model. A Document object denotes the HTML document that is displayed in that window. The document object model consists of several properties that refer to the other objects which give the facility to modify the document content. The process that a content of document is accessed is called the Document Object Model, or DOM. The Objects are organized in a hierarchy. The hierarchical structure uses to the organization of objects in a web document. Below is the hierarchy of the few important DOM objects. We can manipulate the objects and element in the DOM by using the dart:html library. Console-based application cannot use the dart:html library. In work with the HTML library in the web application, we need to import the dart:html. Let’s understand the DOM operation in the following section. Finding DOM Elements A document can contain many attributes sometime we need to search particular attribute. The dart:html library provides the querySelector function to search element in the DOM. The querySelector() function returns the first element that matches the specified group of the selector. Let’s understand the. following syntax. Let’s understand the following example. Example – We create a HTML file name index.html and also create a dart file. Main.dart Event Handling The dart:html library provides the onClick event for DOM Elements. The syntax shows how an element could handle a stream of click events. The querySelector() function returns the element from the given DOM and onClick.listen() will take an eventHandler method which will be invoked when a click event is raised. The syntax of eventHandler is given below ? Let us now take an example to understand the concept of Event Handling in Dart. TestEvent.html TestEvent.dart

October 12, 2024 / 0 Comments
read more

What is Unit Testing?

6. Advance Topics

Unit testing is a part of the software development process where particular unit/components of an application are tested. Unit testing of each component is necessary to enhance the application performance. We can say that a unit is the smallest testable chunk of a program that can be logically isolated in a system. In various programming languages, the individual program, a subroutine, a function, a method, or a class can be represented as a Unit. There can be many individual units within the module. In object-oriented programming, the method that belongs to base class/superclass, an abstract class can be represented as smaller units. Below image displays the type of testing. Unit Testing Task The unit testing task is given below. Advantage of Unit Testing Few advantages of unit testing are given below. Dart Unit Testing In Dart, we required to include an external library named “test”, which facilitates a standard way of writing and running individual unit test. Unit testing can be achieved using the following steps. Step – 1: Installing “test” package To include the unit testing in our project, we must install a third-party packages “test” in our current working project. Let’s open the pubspec.yaml in the our project and type the following statement. Now, right-click on pubspec.yaml file and click on Pub: get dependencies. This will install the “test” package in our project. We can also install it by using the following command. Step – 2: Importing “test” package Type the following line to import the “test” package in your project. Step – 3: Writing Test Cases The top-level test() function is added by the Test cases. In the test() function, we make the test assertion using the expect() function. The expert() function takes two arguments actualValue and MatchValue. Syntax: Group of Test Cases We can create a group of multiple test cases using the group() function. It helps grouping of test cases based on some criteria. The description of each group is specified in the beginning of its test’s description. Syntax: The syntax is given below. Example – 1(Passing Test) Here is the example where we define an add() method for unit test. It accepts two parameters as an integer values and returns an integer representing the sum. To test the add() method, understand the following example – Step – 1: We import the test package. Step -2: We define the test using the test() function and it uses the expert() function to impose an assertion. Output: Example – 2 A unsuccessful Test We define the sub() method which has a logical mistake. Let’s have a following example. Output: In the above example, the add() function is tested successfully but the sub() function failed the unit test due to logical error. Grouping Test Cases We can write the multiple test cases in the form of group. The group() is used to grouped together these methods. It helps to write much cleaner code. In the following example, we are writing a test case for the split() function and the trim() function. We grouped together these function and name it String. Let’s understand the following example – Example – Output:

October 12, 2024 / 0 Comments
read more

What is Concurrency?

6. Advance Topics

The Dart concurrency allows us to run multiple programs or multiple parts of a program simultaneously. It executes the several instructions at the same time. Dart provides the Isolates as a tool for doing works for parallel. The concurrency makes the program highly effective and throughput by utilizing the unused capabilities of essential operating system and machine hardware. How to achieve concurrency? In Dart, we can achieve concurrency by using the Isolates. We have discussed Dart isolates in previous tutorial. Here we will understand the brief introduction of it. Dart isolate is a version of the thread. But there is key difference between the common implementation of “Thread” or “Isolates”. The isolate works differently in comparison of Thread. The isolates are independent workers that do not share memory, but instead interconnect by passing message over channels. Since isolates completes its task by passing message thus it need a way to serialize a message. The communication between the isolates is done by the message passing as a client and server. It helps the program to take advantage of multicore microprocessor out of the box. Dart provides the dart:isolate package to apply the isolate in our program. It provides the solution to taking single-threaded Dart code and allowing application to make greater use of the hardware available. Let’s understand the following example – Example – Output: Output 2: The concept of the above code is similar to the dart isolates. If we run the above program multiple times, then output will be varied every time.

October 12, 2024 / 0 Comments
read more

Async

6. Advance Topics

Dart Async is related to asynchronous programming. It executes the asynchronous operation in a thread. It ensures that the critical functions to be executed until completion. The asynchronous operation is executed, separately the main application thread. In Dart, one operation cannot interrupt the other operation; it means one operation can execute at a time no other part of the program can avert it. Let’s understand the following example – Example – Output Explanation: In the above code, we used the readLineSync(), which is synchronous method. It means; the execution of all instructions that follow the readLineSync() method call will be blocked until the readLineSync() method doesn’t complete its execution. The stdin.readLineSync () executes nothing until it gets the input from the user. It waits for the user input for further execution. Difference between Asynchronous and Synchronous Let’s understand the difference between synchronous and asynchronous. In computer science, if we say a particular program is synchronous, that means it waits for an event to execute further. This approach is driven with a demerit, if a part of the code takes much time to execute, the succeeding blocks through an unrelated block will be blocked from executing. This is the main problem of the synchronous approach. A part of the program may require executing before the current part, but the synchronous approach doesn’t allow it. This is not suitable for the webservers, where request must be independent of the others. It means, the webserver does not wait to finish the execution of the current request, it responds to the request from the other users. The web server should accept the request from the other user before executing the previous requests. This approach is called asynchronous programming. The asynchronous programming generally focuses on no waiting or non-blocking programming model. The dart: async is facilitated to implement the asynchronous programming block in a Dart script. Example – We create a file with the few names and save this file as names.txt and write a program to read this file without delaying the other part of the code. Consider the following code. Output Dart Future The Dart Future is defined as getting a result sometime in the future. The Future object uses to facilitate asynchronous programming. Future objects are a tool to denote values returned by an expression whose execution will complete at a later point in time (In Future). In order to work with the future, we can use either async and await or the Future API. Dart async and await The async and await keywords are allowed to implement asynchronous programming without using the Future API. The async keyword is necessary to run function asynchronously; we need to add async after the function name. The syntax is given below: Syntax: When an async function is invoked, the Future object instantly returns and that indicates the async function will execute later. Once the body of the async function is executed, the function call returned the Future object. The function call will be completed with its result. Dart await Keyword The await keyword is also used to execute function asynchronously. It suspends the currently running function until the result is ready. When it returns the result, then it continues on to the next line of code. The await keyword can only be used with async functions. The syntax is given below. Syntax: Here, e is an asynchronous expression, and it is expected to evaluate to a Future. The await expression evaluates e, and then suspends the currently running function until the result is ready. Let’s understand the following example – Example – 1 Output Explanation: Here, we have declared the main() function asynchronous using the async keyword because we call the hii() method asynchronously. Then, we used await modifier to call hii() that executed asynchronously.

October 12, 2024 / 0 Comments
read more

Isolates

6. Advance Topics

Dart allows us to asynchronous programming which runs our program without getting blocked. The asynchronous programming is used to achieve concurrency. Dart isolate is a version of the thread. But there is key difference between the common implementation of “Thread” or “Isolates”. The isolate works differently in comparison of Thread. The isolates are independent workers that do not share memory, but instead interconnect by passing message over channels. Since isolates completes its task by passing message thus it need a way to serialize a message. The communication between the isolates is done by the message passing as a client and server. It helps the program to take advantage of multicore microprocessor out of the box. Dart provides the dart:isolate package to apply the isolate in our program. It provides the solution to taking single-threaded Dart code and allowing application to make greater use of the hardware available. Create and Start an Isolate Dart provides the spawn() method to create an isolate. It must be declared with an ‘entry point’ with a single parameter. This parameter displays a port which isolate use to refer back notification message. Let’s understand the following example – Example – Output: Output 2: Explanation: In the above program, the spawn method of the isolate class executed a function sayhii in parallel of remaining code. It takes two parameters. We have two functions sayhii() and main() function might not run in the same order each time. If you run the above program, then the output will be different each time as we can see in second output. Note – We can also pass NULL value if there is no object to pass in spawned function. Let’s understand the another example – Example – 2 Explanation: In the above code, we created an asynchronous method start() which creates a port and spawn an isolate. We signified the start method as async because of wan can await the response from the spawning of the isolates and to store a reference to the new isolate. It is essential when we want to kill the running isolates. We passed the two parameters in the spawn() method, the first parameter runTimer method, that is a callback function to execute runTimer() and second parameter sendPort which is a callback function and it will used to send message back to the caller. The start() method starts listening the receiverPort for message from isolate. Once it will receive the message then it will print as a console output. The runTimer() method begins a timer that fires every second in order to update a counter. It sends a notification message via the port which it received when the isolate was spawned. Stop an Isolate The dart: isolates package provides the kill() method which is used to stop a running isolate. Let’s understand the following example. Example – Explanation: In the above example, we have declared a stop() method that will destroy the running isolate and sets its reference to null. We defined the priority of isolate: immediate that will terminate the isolate as soon as. Complete Program Output:

October 12, 2024 / 0 Comments
read more

Callable Classes

6. Advance Topics

Dart provides the facility to call class instances like a function. To make callable class, we need to implement a call() method in it. Let’s understand the following example – Example – 1 Output Explanation: In the above code, We defined a call() function in the class Student that takes two arguments String name, integer age and return a message with this information. Then, we have created the object of class Student and called it like a function. Let’s have a look at another example – Example – 2 Multiple callable class Output Explanation: In the above code, we defined two callable functions in class Student and class Employee. The Employee class call() function accepts two parameters as String empid and int age. We called instances of both classes as callable functions.

October 12, 2024 / 0 Comments
read more

Posts pagination

Previous 1 … 112 113 114 … 445 Next