Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. Today I will share with you a few tips for flutter development

Improve the success rate of the Flutter attach

Plan 1

Disconnect the wifi, execute the flutter attach, and connect to the wifi after the attach succeeds

Scheme 2

Attach by specifying the device UUID

  1. throughflutter devicesQuery the linked device list to obtain the UUID of the linked device
  2. flutter attach -d <#uuid#>Specify the device you want to attach for connection, for exampleflutter attach -d AE5D772C-6D56-43AD-83F2-0554257B16C4

Plan 3

Attach by specifying the app-ID of the application

  1. If multiple devices of the same project are connected in the same wifi environment, find the app-ID you need to attach for connection
  2. Perform:flutter attach --app-id <#app_id#>Such as:flutter attach --app-id com.xxx.xxx

Load the JSON file from the package

background

For example, I have a flutter demo project called Flutter_demo_package_json_load in the root directory with the command: Flutter create –template=package json_test_package, create a package named json_test_package. Now you need to load the change file in the package.

Load JSON from the project

For json files of common projects, the loading method is as follows:

  1. Add a JSON file to the project. For example, there is a file named test1.json under assets of the project

  2. We added the following dependencies to YAML

    Assets: -assets /test1.json # or # Load all assets: -assets /Copy the code
  3. Load through rootBundle

    Future<String> _loadFromProjectAsset() async {
      // json file in project,is ok
      return rootBundle.loadString("assets/test1.json");
    }
    Copy the code

However, for the JSON file under package, the above method is invalid. Using the above method to load will bring an error message as follows:

The error is that the resource path is incorrect and cannot be loaded. Because the rootBundle class does not provide package name parameter, we can only put package name information in the resource path.

Load JSON from package

Inspired by pub.dev/packages/lo… : “package_name”) ‘method, because Lottie also implements animation by loading a JSON configuration, but this method supports specifying a package configuration.

KeyName is the resource path we need to use in the rootBundle class, including the package case.

The correct way is as follows:

  1. The first step is to add the JSON file to the project as above, assuming that the JSON file in our package is named test2.json

  2. Add the following dependencies in YAML

    Assets: -assets /test2.json # or # Load all assets: -assets /Copy the code
  3. Packages /<#package_name#>/<#file_path#>

Here for

// json file in package,this is work Future<String> _loadFromPackageAsset() async {// 【importance】 packag json file need use this formate: // `packages/$package/$assetName` return rootBundle.loadString("packages/json_test_package/assets/test2.json"); }Copy the code

You can do the same thing with DefaultAssetBundle

/ / This is the work too. The Future < String > testLoadJsonFileByDefaultAssetBundle (BuildContext context) {/ / importance of packag 】 json file need use this formate: // `packages/$package/$assetName` return DefaultAssetBundle.of(context) .loadString('packages/json_test_package/assets/test2.json'); }Copy the code

The Demo link