Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to avoid Project failure in Flutter Project Development

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to avoid project failure in Flutter project development, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to avoid project failure in Flutter project development. Let's take a look.

Overview

Before we explain how to create custom packages, let's review why Flutter developers need to use them and how they are used. The goal of the package is to isolate reusable functionality and make it easily accessible to the rest of the application. A single application can have multiple custom packages.

For example, we can create a custom package to handle HTTP network API connections with different buttons to display the results to different screens. A good way to organize code is to isolate all connection functions into a connection. The connection directory becomes the connection package. You can also create another package for the animation and organize custom animations in that package. Each package you create is placed in a file called packages.

Once the code is isolated in the package, you can make the package accessible to your application in the main project pubspec.yaml application configuration file. Pubspec.yaml depending on how you distribute the package, the syntax of the file is slightly different.

Once used to make the package available to applications using pubspec.yaml, each file that uses the package imports the package import 'package:name/ file.dart` using the following syntax

Let's illustrate this process with an example, starting with the pubspec.yaml configuration.

Three types of package dependent configuration formats

There are three strategies for building and distributing custom Flutter packages:

Publish to pub.dev

Publish to GitHub or equivalent stand-alone repository

Store the package in your main project repository.

1. Pub.dev

If you publish the package to pub.dev, you can access it from pubspec.yaml, under dependencies. Since my library is not on pub.dev, I will show the syntax of the HTTP package.

YAML:

Dependencies: http: ^ 0.13.32. Remote Git server

In most cases, you don't need to make your package accessible to many people. In these cases, you can access the library from the network using the following syntax.

YAML:

Dependencies: theta: url: https://github.com/codetricity/theta ref: mai

In the above example, the package name is theta. This package is available under the main branch on GitHub.

3. Packages folder of the project

You can also store the package in the main application project folder. If the package name is theta, it is stored in the main project directory packages/theta.

If the package is stored locally, the pubspec.yaml syntax is as follows.

YAML:

Dependencies: theta: path: packages/theta uses custom packages

In all three ways, you can import a custom package into your application to use it.

Application code

Dart:

Import 'package:theta/theta.dart';void main (List args) async {print (await Camera.info);} Custom Encapsulation Code

To show which part of the code comes from the package, I will include a short section of the library.

In the above example, this part of the Camera.info is a static gettertheta/lib/src/protocols.dart located in the package

Dart:

Const String _ baseUrl = 'http://192.168.1.1/osc/';class Camera {static Future get info async {var url = _ baseUrl +' info'; var response = connect (url, 'get'); return response;}.

After you create a package, you can easily access all the classes and methods in the package.

Distribution strategy suggestion

You should first place your package in the application repository packages directory, and initially use the package for only one of your applications. Once the application and package are stable, you can distribute them to the GitHub for use by other members of the team or organization directly from their pubspec.yaml files.

After you make the package accessible to other groups and they start using it, you will need to spend more time on package versioning.

Create a package

Create a new project. I call my project theta_api_intro_tutorial.

Create a folder called packages in the top-level directory of the main project.

Change to the directory and create a new package. In this example, my package name is theta_connection.

Flutter create-template=package theta_connection

When you create a package flutter create, it automatically creates another set of lib,pubspec.yaml and README.md files.

In the newly created lib directory, there is a main theta_connection.dart file that serves as the central connection point for the rest of the files in the package.

Remove the sample code from the theta_connection.dart and test/theta_connection.dart files.

Part

The first way to create a new file to save the new package. I have called my file protocols/test_hello.dart.

The key learning point for this file is to learn how to include part of'.. / theta_connection.dart' to the top of the file. This is the connection point from each file in the package to the main package file, which will allow your application to access this method later.

Part

In addition to using the to method to connect files to theta_connection.dart, you also need to add a connection from theta_connection.darttoprotocols/test_hello.dart

Congratulations, you just created a custom package!

Step review

Create a package with the name flutter create-- template=package.

Create a new file for your package functionality and link it to the main package file part of.

In the main package file, link to the file with part.

Use your new bag

In the file in the pubspec.yaml main project directory, add the path to the new package.

Import the package main.dart and use it hello () by calling the method.

When you press the button, you should see "Hello, world" in the console.

Expand your package

Using this technique, you can add additional directories and files to each new directory. You can also create different packages for different types of functionality.

As the application grows, you will need to manage the state of the application. My previous article, Flutter 2.0 State Management Introduction with Provider 5.0, explained how to use provider to manage the state of Flutter applications.

Generalization

Strong application architecture organization is the key to the success of the project. Failure to manage basic areas such as functionality and status can lead to delays and, in some cases, unfinished projects. The effective use of the standard Flutter package system is an excellent tool for organizing application architecture. To keep development simple, put your package in the main application repository to reduce the complexity of package version management. By reducing the complexity of package versioning, you and your team will increase your chances of using packages and put everyone on the path to successfully starting the project.

This is the end of the article on "how to avoid project failure in Flutter project development". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to avoid project failure in Flutter project development". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report