Tag Archives: Flutter

[Solved] Flutter Error: fluwx:compileDebugKotlin

Flutter reports an error fluwx:compileDebugKotlin

Cause Analysis

The reason for this exception found on the Internet is basically that the kotlin version does not conform to the cause, Maybe the kotlin version will also cause this error.
But my error is not due to the kotlin version problem, but due to the conflict of the component cache version pulled down by the pub command, as shown below:
insert picture description here
fluwx-3.9.1 and 3.9.2, delete and yml file Inconsistent versions resolve this exception.

Summary

Component name: There may be two reasons for the exception of compileDebugKotlin:
1. The kotlin version often said on the Internet does not conform to
2. There are different versions of the components in the pub cache and they conflict

pod install error: Oh no, an error occurred. (Ultimate Solution)

Project scenario:

There was an error in pod install recently

[!] Oh no, an error occurred.


Problem description


JSON::ParserError - 416: unexpected token at '"SharedTestUtilities/FIROptionsMock'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/json/common.rb:156:in `parse'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/json/common.rb:156:in `parse'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/specification/json.rb:61:in `from_json'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/specification.rb:748:in `from_string'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/specification.rb:722:in `from_file'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/source.rb:188:in `specification'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/specification/set.rb:58:in `block in specification_name'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/specification/set.rb:56:in `each'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/specification/set.rb:56:in `specification_name'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/cdn_source.rb:216:in `search'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/source/aggregate.rb:83:in `block in search'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/source/aggregate.rb:83:in `select'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/source/aggregate.rb:83:in `search'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:416:in `create_set_from_sources'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:385:in `find_cached_set'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:360:in `specifications_for_dependency'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:165:in `search_for'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:274:in `block in sort_dependencies'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:267:in `each'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:267:in `sort_by'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:267:in `sort_by!'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:267:in `sort_dependencies'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/delegates/specification_provider.rb:60:in `block in sort_dependencies'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/delegates/specification_provider.rb:77:in `with_no_such_dependency_error_handling'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/delegates/specification_provider.rb:59:in `sort_dependencies'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:754:in `push_state_for_requirements'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:744:in `require_nested_dependencies_for'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:727:in `activate_new_spec'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:684:in `attempt_to_activate'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:254:in `process_topmost_state'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolution.rb:182:in `resolve'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/molinillo-0.8.0/lib/molinillo/resolver.rb:43:in `resolve'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/resolver.rb:94:in `resolve'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/installer/analyzer.rb:1078:in `block in resolve_dependencies'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/user_interface.rb:64:in `section'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/installer/analyzer.rb:1076:in `resolve_dependencies'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/installer/analyzer.rb:124:in `analyze'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/installer.rb:416:in `analyze'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/installer.rb:241:in `block in resolve_dependencies'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/user_interface.rb:64:in `section'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/installer.rb:240:in `resolve_dependencies'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/installer.rb:161:in `install!'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/command/install.rb:52:in `run'
/Users/tiger/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/claide-1.0.3/lib/claide/command.rb:334:in `run'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/lib/cocoapods/command.rb:52:in `run'
/Users/tiger/.rvm/gems/ruby-2.6.3/gems/cocoapods-1.11.3/bin/pod:55:in `<top (required)>'
/Users/tiger/.rvm/gems/ruby-2.6.3/bin/pod:23:in `load'
/Users/tiger/.rvm/gems/ruby-2.6.3/bin/pod:23:in `<main>'
/Users/tiger/.rvm/gems/ruby-2.6.3/bin/ruby_executable_hooks:24:in `eval'
/Users/tiger/.rvm/gems/ruby-2.6.3/bin/ruby_executable_hooks:24:in `<main>'

――― TEMPLATE END ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

[!] Oh no, an error occurred.

Search for existing GitHub issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=416%3A+unexpected+token+at+%27%22SharedTestUtilities%2FFIROptionsMock%27&type=Issues

If none exists, create a ticket, with the template displayed above, on:
https://github.com/CocoaPods/CocoaPods/issues/new

Be sure to first read the contributing guide for details on how to properly submit a ticket:
https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md

Don't forget to anonymize any private data!

Looking for related issues on cocoapods/cocoapods...
Found no similar issues. To create a new issue, please visit:
https://github.com/cocoapods/cocoapods/issues/new


Cause analysis:

In fact, after upgrading cocoapods, you need to clear it…


Solution:

Use this following command to clear the cache:

sudo rm -rf ~/.cocoapods/repos

[Solved] Virtual Machine Error: FAILURE: Build failed with an exception.Flutter

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> Could not resolve all dependencies for configuration ':app:debugRuntimeClasspath'.
   > Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven(http://storage.flutter-io.cn/download.flutter.io)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.4/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details. 

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 50s
Exception: Gradle task assembleDebug failed with exit code 1

 

Solution:

Although there are many tutorials online, but there is no solution to this solution, the early my attention are in the first sentence, only to know that the original focus is the latter sentence, the main error is in the http and https protocol, Just modify HTTP to HTTPS of the system variable FLUTTER_STORAGE_BASE_URL variable value.

[Solved] FlutterIOS CDN trunk Repo update failed – 34 error(s)

FlutterIOS CDN trunk Repo update failed – 34 error(s)

Add the following codes behind the Podfile:
[!] CDN: trunk Repo update failed – 34 error(s):

source 'https://github.com/CocoaPods/Specs.git'

First, you have to execute the following commands

git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/master

Then view the pod repo list

remove the selected
by executing pod repo remove trunk. Finally, execute pod install

If this problem occurs

just change the platform in podfile: IOS, ‘13.0’
adapt to the IOS version of cocoapods

[Solved] Failed assertion: line 3180 pos 12: ‘debugNeedsPaint‘: is not true

Click the screenshot of the flitter to call the screenshot method for the first time, and an error is reported
Failed assertion: line 3180 pos 12: ‘debugNeedsPaint’: is not true.

flutter: 'package:flutter/src/rendering/proxy_box.dart': Failed assertion: line 3180 pos 12: '!debugNeedsPaint': is not true.

Solution:
Add a delay call when calling the screenshot method

 onPressed: () {
                Future.delayed(Duration.zero, () {
                  this._capturePng();
                });
              },

[Solved] Execution failed for task ‘:app:checkDebugAarMetadata‘

After updating Fuller, the following problems will be reported:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> Multiple task action failures occurred:
   > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
      > The minCompileSdk (31) specified in a
        dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
        is greater than this module's compileSdkVersion (android-30).
        Dependency: androidx.window:window-java:1.0.0-beta04.
        AAR metadata file: C:\Users\liqiang\.gradle\caches\transforms-2\files-2.1\9f515ee58db509b5f4759e97c8eb6aa2\jetified-window-java-1.0.0-beta04\META-INF\com\android\build\gradle\aar-metadata.properties.
   > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
      > The minCompileSdk (31) specified in a
        dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
        is greater than this module's compileSdkVersion (android-30).
        Dependency: androidx.window:window:1.0.0-beta04.
        AAR metadata file: C:\Users\liqiang\.gradle\caches\transforms-2\files-2.1\a909ff21160c236fa8213aba5c707997\jetified-window-1.0.0-beta04\META-INF\com\android\build\gradle\aar-metadata.properties.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 4s
Exception: Gradle task assembleDebug failed with exit code 1

According to the error message The minCompileSdk (31) specified, the final sdk needs to be version 31, but our current sdk version is 30, which is greater than this version, so we need to modify the following configuration to 31

android {
    compileSdkVersion 31

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        /*configurations.all {
            resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
        }*/
        applicationId "cn.liginfo.kqjhq_app"
        minSdkVersion 16
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

After modification, because there is no version 31 sdk, it will download automatically, but after downloading, the following error message appears again

Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Launching lib\main.dart on sdk gphone x86 arm in debug mode...
Running Gradle task 'assembleDebug'...
e: C:/Users/liqiang/.gradle/caches/transforms-2/files-2.1/0dd39ff9faeeb501ffb52162e269003e/jetified-kotlin-stdlib-1.5.31.jar!/META-INF/kotlin-stdlib.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
e: C:/Users/liqiang/.gradle/caches/transforms-2/files-2.1/2ea246a87a592f122121cfd0846ffbea/jetified-kotlin-stdlib-jdk7-1.5.30.jar!/META-INF/kotlin-stdlib-jdk7.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
e: C:/Users/liqiang/.gradle/caches/transforms-2/files-2.1/9234cfcee40ec23fc2213363e9726513/jetified-window-1.0.0-beta04-api.jar!/META-INF/window_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
e: C:/Users/liqiang/.gradle/caches/transforms-2/files-2.1/9af1d6ff9000d18681a83cd875960957/jetified-kotlinx-coroutines-core-jvm-1.5.2.jar!/META-INF/kotlinx-coroutines-core.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
e: C:/Users/liqiang/.gradle/caches/transforms-2/files-2.1/b0b1eecff74e71ed1ef092403ad1018c/jetified-kotlin-stdlib-common-1.5.31.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
e: C:/Users/liqiang/.gradle/caches/transforms-2/files-2.1/d91778f7878bf943ae593906c9fd7c75/jetified-kotlin-stdlib-jdk8-1.5.30.jar!/META-INF/kotlin-stdlib-jdk8.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
e: C:/Users/liqiang/.gradle/caches/transforms-2/files-2.1/df6880e9350e429186aa0973bbf49093/jetified-window-java-1.0.0-beta04-api.jar!/META-INF/window-java_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.
e: C:/Users/liqiang/.gradle/caches/transforms-2/files-2.1/e484fc5cda5fe73d156ca3b50e47d38e/jetified-kotlinx-coroutines-android-1.5.2.jar!/META-INF/kotlinx-coroutines-android.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 7s

┌─ Flutter Fix ────────────────────────────────────────────────────────────────────────────────┐
│ [!] Your project requires a newer version of the Kotlin Gradle plugin.                       │
│ Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then │
│ update D:\javadeveloping\Android\workspace\kqjhq_app\android\build.gradle:                   │
│ ext.kotlin_version = '<latest-version>'                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────┘
Exception: Gradle task assembleDebug failed with exit code 1

From the error message, it is said that the version of kotlin is wrong, and a specific solution is given

Your project requires a newer version of the Kotlin Gradle plugin.                       │
│ Find the latest version on https://kotlinlang.org/docs/gradle.html#plugin-and-versions, then │
│ update D:\javadeveloping\Android\workspace\kqjhq_app\android\build.gradle:                   │
│ ext.kotlin_version = '<latest-version>'   

You need an up-to-date plugin named kotlin gradle, Find the recent version in https://kotlinlang.org/docs/gradle.html#plugin-and-versions and update the the latest-version value in ext.kotlin_version = ‘’ of the D:\javadeveloping\Android\workspace\kqjhq_app\android\build.gradle file.

after opening the web page, we found that the latest version is 1.6.10, while our previous version is 1.3.5:

just change to 1.6.10 and package normally

Could not identify launch activity: Default Activity not foundError while Launching activity

Add in activity

<intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

When the app has multiple activities, the activity to be started is the main interface and is set to MAIN.

LAUNCHER indicates whether it is displayed in the mobile app list.

[Solved] flutter Project Error: ScrollController attached to multiple scroll views, Failed assertion: line 109 pos 12

1. Explain

Recently, I encountered an error when writing the shuttle project:

The following assertion was thrown while handling a gesture:
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 109 pos 12: '_positions.length == 1'

Reasons for error reporting:
1 The drawing function added to the project needs to navigate to different pages through the menu items of the drawing
2. A bottom navigation bar is added to the home page to navigate to different pages through the pagecontroller
3. The project manages routing and status through geTx.

Description of the problem: navigate to the settings or other pages through the drawer, and then re-navigate to the home page through the drawer. Click the item item item of the BottomNavigationBar, and will appear the error messages:

Failed assertion: line 109 pos 12: ‘_positions.length == 1’


2. Solution:

1. Drawer navigation uses Get.offAndToNamed(‘12313’); to jump.
2. You need to reinitialize the pageController every time you jump to the homepage. Through the build method of GetView, the controller is reinitialized every time the homepage interface is redrawn.

Specific operation:

code for the navigation part of the drawer:

class AppRouteProvide {
  Future onRouteTo(String routeName) {
    switch (routeName) {
      case Routes.Auth:
        Get.toNamed(Routes.Auth);
        break;
      case Routes.Home:
        Get.offAndToNamed(Routes.Home);
        break;
      case Routes.Help:
        Get.offAndToNamed(Routes.Help);
        break;
      case Routes.Splash:
        Get.toNamed(Routes.Splash);
        break;
      case Routes.Connect:
        Get.offAndToNamed(Routes.Connect);
        break;
      case Routes.Setting:
        Get.offAndToNamed(Routes.Setting);
        break;
    }
    return Future.value();
  }
}

Code of home page view:

class HomeScreen extends GetView<HomeController> {
  @override
  Widget build(BuildContext context) {
    // print('HomeScreen build');
    if (controller.pageController.hasClients) {
      controller.onClose();
      controller.onInit();
    }
    return AppbarWidgetScreen(
      body: _buildBody(context),
      bottomBar: _buildBottomNavigationBar(context),
    );
  }

Home page controller part code


class HomeController extends GetxController {
  final ApiRepository apiRepository;
  RxInt selectedIndex = 0.obs;

  late PageController pageController;
  RxList<Widget> widgetPage = <Widget>[
    HomeMainScreen(),
    HomeBuyScreen(),
    HomeCertScreen(),
    HomeSellScreen(),
    HomeMineScreen(),
  ].obs;

  HomeController({required this.apiRepository});

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onInit() {
    print('HomeController onInit()');
    pageController = PageController(
      initialPage: selectedIndex.value,
      keepPage: true,
    );
    super.onInit();
  }

  @override
  void onClose() {
    super.onClose();
  }

[Solved] Flutter compile error: Failed to find Build Tools revision 29.0.2

Could not determine the dependencies of task ‘:app:compileDebugJavaWithJavac’.
> Failed to find Build Tools revision 29.0.2

This question:

1. Is this version available in the build tools in the SDK? If not, install

2. Your sdk directory has been modified, if you modify it, you need to re-define the sdk path for flutter

flutter config --android-sdk E:/Android_SDK

The E:/Android_SDK at the back is my modified sdk path. After running, restart the compiler and you can compile normally in flutter run

[Solved] Flyter uses screenutil to obtain screen width and height initialization error

Errors are reported as follows

reason

We use screenutil().Screenwidth to obtain the screen width in the layout. When initializing the unloaded view, we can’t get it for the first time, and the above crash will occur.

Solution

import 'package:flutter_screenutil/screenutil_init.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
@override
Widget build(BuildContext context) {
  //Wrap the entire body layout with screen initialization
  return ScreenUtilInit(
    //set initial screen size
    designSize: Size(375, 812),
    builder: () => Scaffold(
      backgroundColor: WBColors.color_f4f5f7,
      body: Container(
          height: 263.5,
          width: ScreenUtil().screenWidth,
        )
    )
  );
}

Use the screenutilinit screen initialization component to wrap the outermost layer of the entire page layout and set the initial screen size. The API for obtaining screen width and height can be used arbitrarily in the body