Dart 2 (2018)

3. History

The introduction of Dart 2 brought major changes, including a more powerful type system and improved performance. Dart 2 emphasized sound null safety, making it easier for developers to avoid null-related bugs.

October 16, 2024 / 0 Comments
read more

Creating Android Platform-Specific Code

5. Advanced Concepts

In this section, we are going to see how we can write custom platform-specific code in Flutter. Flutter is an excellent framework, which provides a mechanism to handle/access platform-specific features. This feature allows the developer to extend the functionality of the Flutter framework. Some of the essential platform-specific functionality that can be accessed easily through the framework are camera, battery level, browser, etc. Flutter uses a flexible system to call platform-specific API either available on Android in Java or Kotlin code, or in Objective-C or Swift code on iOS. The general idea of accessing the platform-specific code in Flutter is through the messaging protocol. The messages are passed between the client (UI) and Host (Platform) using the common message channel. It means the clients sends a message to the Host by using this message channel. Next, the Host listens on that message channel, receives the message, does the appropriate functionality, and finally returns the result to the client. The following block diagram shows the appropriate platform-specific code architecture. The messaging channel uses a standard message codec (StandardMessageCodec class), which supports efficient binary serialization of JSON like values, such as string, Boolean, numbers, byte buffers, and List and Maps, etc. The serialization and deserialization of these values work automatically between clients and hosts when you send and receive values. The following table shows how dart values are received on Android and iOS platform and vice-versa: Dart Android iOS null null nil (NSNull when nested) bool java.lang.Boolean NSNumber numberWithBool: int java.lang.Integer NSNumber numberWithInt: double java.lang.Double NSNumber numberWithDouble: String java.lang.String NSString: Uint8List byte[] FlutterStandardTypedData typedDataWithBytes: Int32List int[] FlutterStandardTypedData typedDataWithInt32: Int64List long[] FlutterStandardTypedData typedDataWithInt64: Float64List double[] FlutterStandardTypedData typedDataWithFloat64: List java.util.ArrayList NSArray Map java.util.HashMAp NSDictionary Let us create a simple application to demonstrate how to call a platform-specific API to open a browser. To do this, we need to create a Flutter project in Android Studio and insert the following code in the main.dart file. In the above file, we have imported a service.dart file, which includes the functionality to invoke the platform-specific code. In the MyHomePage widget, we have created a message channel and write a method _openBrowser to invoke the platform-specific code. Finally, we have created a button to open the browser. Now, we need to provide the custom platform-specific implementation. To do this, navigate to the Android folder of your Flutter project and select the Java or Kotlin file and insert the following code into the MainActivity file. This code might change according to the Java or Kotlin language. In the MainActivity.kt file, we have created a method openBrowser() to open the browser. Output Now, run the app in your android studio, you will get the following output. When you click on the button Click Here, you can see that the browser home page screen is launched.

October 16, 2024 / 0 Comments
read more

Dart SDK (2013)

3. History

In 2013, the Dart SDK was made available, allowing developers to compile Dart code to JavaScript. This was a significant step for Dart as it enabled developers to use Dart in existing web applications.

October 16, 2024 / 0 Comments
read more

First Release (2011)

3. History

Dart 1.0 was released in November 2011. This version included features such as a strong static type system, classes, interfaces, and a garbage collector. The language was designed for both client-side and server-side development.

October 16, 2024 / 0 Comments
read more

Initial Development (2010)

3. History

Dart was first introduced by Google in 2010 as a response to the challenges of web development, particularly with JavaScript’s limitations. The goal was to create a language that was easier to use and offered better performance.

October 16, 2024 / 0 Comments
read more

Navigation and Routing

4. Flutter Routing

Navigation and routing are some of the core concepts of all mobile application, which allows the user to move between different pages. We know that every mobile application contains several screens for displaying different types of information. For example, an app can have a screen that contains various products. When the user taps on that product, immediately it will display detailed information about that product. In Flutter, the screens and pages are known as routes, and these routes are just a widget. In Android, a route is similar to an Activity, whereas, in iOS, it is equivalent to a ViewController. In any mobile app, navigating to different pages defines the workflow of the application, and the way to handle the navigation is known as routing. Flutter provides a basic routing class MaterialPageRoute and two methods Navigator.push() and Navigator.pop() that shows how to navigate between two routes. The following steps are required to start navigation in your application. Step 1: First, you need to create two routes. Step 2: Then, navigate to one route from another route by using the Navigator.push() method. Step 3: Finally, navigate to the first route by using the Navigator.pop() method. Let us take a simple example to understand the navigation between two routes: Create two routes Here, we are going to create two routes for navigation. In both routes, we have created only a single button. When we tap the button on the first page, it will navigate to the second page. Again, when we tap the button on the second page, it will return to the first page. The below code snippet creates two routes in the Flutter application. Navigate to the second route using Navigator.push() method The Navigator.push() method is used to navigate/switch to a new route/page/screen. Here, the push() method adds a page/route on the stack and then manage it by using the Navigator. Again we use MaterialPageRoute class that allows transition between the routes using a platform-specific animation. The below code explain the use of the Navigator.push() method. Return to the first route using Navigator.pop() method Now, we need to use Navigator.pop() method to close the second route and return to the first route. The pop() method allows us to remove the current route from the stack, which is managed by the Navigator.To implement a return to the original route, we need to update the onPressed() callback method in the SecondRoute widget as below code snippet: Now, let us see the full code to implement the navigation between two routes. First, create a Flutter project and insert the following code in the main.dart file. Output When you run the project in the Android Studio, you will get the following screen in your emulator. It is the first screen that contains only a single button. Click the button Click Here, and you will navigate to a second screen as below image. Next, when you click on the button Go Back, you will return to the first page. Navigation with Named Routes We have learned how to navigate to a new screen by creating a new route and manage it by using the Navigator. The Navigator maintains the stack-based history of routes. If there is a need to navigate to the same screen in many parts of the app, this approach is not beneficial because it results in code duplication. The solution to this problem can be removed by defining the named routes and can use the named routes for navigation. We can work with named routes by using the Navigator.pushNamed() function. This function takes two required arguments (build context and string) and one optional argument. Also, we know about the MaterialPageRoute, which is responsible for page transition. If we do not use this, then it is difficult to change the page. The following steps are necessary, which demonstrate how to use named routes. Step 1: First, we need to create two screens. The following code creates the two screens in our app. Step 2: Define the routes. In this step, we have to define the routes. The MaterialApp constructor is responsible for defining the initial route and other routes themselves. Here the initial route tells the start of the page and routes property defines the available named routes and widgets. The following code explains it more clearly. Step 3: Navigate to the second screen using the Navigator.pushNamed() function. In this step, we need to call Navigator.pushNamed() method for navigation. For this, we need to update an onPressed() callback in the build method of HomeScreen like below code snippets. Step 4: Use a Navigator.pop() function to return to the first screen. It is the final step, where we will use Navigator.pop() method to return on the first screen. Let us see the full code of the above explanation in the Flutter project and run it in the emulator to get the output. Output Click the button Click Here, and you will navigate to a second screen as below image. Next, when you click on the button Go Back, it will return to the Home Page.

October 16, 2024 / 0 Comments
read more

Flutter Dependency

2. Disadvantage

Dart’s strong integration with Flutter can be a disadvantage if developers prefer other frameworks or platforms. Using Dart outside the Flutter ecosystem may not offer the same level of support and resources.

October 16, 2024 / 0 Comments
read more

Learning Curve for Existing Developers

2. Disadvantage

Developers with experience in other languages may need to invest time in learning Dart’s syntax, features, and best practices. Adapting to Dart’s ecosystem and tools can also pose a learning curve.

October 16, 2024 / 0 Comments
read more

Limited Adoption

2. Disadvantage

While Dart has gained popularity, it still lags behind other programming languages like JavaScript in terms of adoption and community size. This may result in fewer resources and community support compared to more widely adopted languages.

October 16, 2024 / 0 Comments
read more

Growing Community and Ecosystem

1. Advantage

Dart has a growing community of developers and a vibrant ecosystem with libraries, frameworks, and tools. This provides support, resources, and opportunities for collaboration and knowledge sharing.

October 16, 2024 / 0 Comments
read more

Posts pagination

Previous 1 … 108 109 110 … 445 Next