导入dart_eval包未调用方法即触发内置代码错误求助
Flutter导入dart_eval包未调用方法就触发内置代码错误
测试代码
import 'package:dart_eval/dart_eval.dart'; import 'dart:math'; void main() { // print(eval('2 + 2')); // -> 4 print('hi'); }
错误信息
/D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:41:13: Error: Expected an identifier, but got ']'. Try inserting an identifier before ']'. ])?.$value; ^ /D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:41:13: Error: Expected ':' before this. ])?.$value; ^ /D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:40:44: Error: A value of type '$Value' can't be assigned to a variable of type 'bool'. - '$Value' is from 'package:dart_eval/src/eval/runtime/class.dart' ('/D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/runtime/class.dart'). runtime.wrapPrimitive(value) ?? value as $Value? ^ /D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:104:103: Error: Expected an identifier, but got ']'. Try inserting an identifier before ']'. return toEncodable.call(runtime, null, [runtime.wrapPrimitive(object) ?? object as $Value?])?.$value; ^ /D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:104:103: Error: Expected ':' before this. return toEncodable.call(runtime, null, [runtime.wrapPrimitive(object) ?? object as $Value?])?.$value; ^ /D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:104:83: Error: A value of type '$Value' can't be assigned to a variable of type 'bool'. - '$Value' is from 'package:dart_eval/src/eval/runtime/class.dart' ('/D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/runtime/class.dart'). return toEncodable.call(runtime, null, [runtime.wrapPrimitive(object) ?? object as $Value?])?.$value; ^ /D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:171:17: Error: Expected an identifier, but got ']'. Try inserting an identifier before ']'. ])?.$value; ^ /D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:171:17: Error: Expected ':' before this. ])?.$value; ^ /D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/shared/stdlib/convert/json.dart:170:48: Error: A value of type '$Value' can't be assigned to a variable of type 'bool'. - '$Value' is from 'package:dart_eval/src/eval/runtime/class.dart' ('/D:/Flutter%20SDK/flutter/.pub-cache/hosted/pub.dartlang.org/dart_eval-0.6.0/lib/src/eval/runtime/class.dart'). runtime.wrapPrimitive(value) ?? value as $Value? ^
已尝试操作
- 执行
flutter pub upgrade dart_eval,确认dart_eval已是最新的^0.6.0版本
环境配置
pubspec.yaml
name: app description: A new Flutter project. publish_to: 'none' version: 1.0.0+1 environment: sdk: '>=2.18.4 <3.0.0' dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 http: ^0.13.5 dart_eval: ^0.6.0 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.0 flutter: uses-material-design: true
Flutter Doctor输出
[√] Flutter (Channel stable, 3.3.8, on Microsoft Windows [Version 10.0.22621.1555], locale en-US) • Flutter version 3.3.8 on channel stable at D:\Flutter SDK\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 52b3dc25f6 (5 months ago), 2022-11-09 12:09:26 +0800 • Engine revision 857bd6b74c • Dart version 2.18.4 • DevTools version 2.15.0 [X] Android toolchain - develop for Android devices X Unable to locate Android SDK. Install Android Studio from: https://developer.android.com/studio/index.html On first launch it will assist you in installing the Android SDK components. (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions). If the Android SDK has been installed to a custom location, please use `flutter config --android-sdk` to update to that location. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [X] Visual Studio - develop for Windows X Visual Studio not installed; this is necessary for Windows development. Download at https://visualstudio.microsoft.com/downloads/. Please install the "Desktop development with C++" workload, including all of its default components [!] Android Studio (not installed) • Android Studio not found; download from https://developer.android.com/studio/index.html (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions). [√] VS Code (version 1.77.3) • VS Code at C:\Users\hanno\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.62.0 [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22621.1555] • Chrome (web) • chrome • web-javascript • Google Chrome 112.0.5615.86 • Edge (web) • edge • web-javascript • Microsoft Edge 112.0.1722.39 [√] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 3 categories.
问题询问
该错误是否与Flutter环境相关?如何解决以在Flutter应用中正常使用dart_eval包?
解决方案
这些错误是dart_eval 0.6.0版本内部代码的语法和类型不兼容问题,和你当前的Dart版本(2.18.4)有关,并非Flutter环境本身的问题,可通过以下方式解决:
升级dart_eval到预发布版本
在pubspec.yaml中将dart_eval依赖改为预发布版本,比如:dart_eval: ^0.7.0-dev.1预发布版本通常修复了旧版本的兼容性问题,适配更高版本的Dart语法。
升级Flutter到较新稳定版
当前你使用的Flutter 3.3.8对应Dart 2.18.4,版本较旧。升级到Flutter 3.10+稳定版(对应Dart 3.0+),新版本的Dart语法解析更完善,能兼容dart_eval 0.6.0的代码写法。手动修复dart_eval包内的错误
若暂时无法升级环境,可手动修改缓存中的dart_eval代码:- 打开报错的
json.dart文件,将类似[runtime.wrapPrimitive(value) ?? value as $Value?]的代码,改为[(runtime.wrapPrimitive(value) ?? value as $Value?)],用括号包裹??表达式,解决语法解析错误; - 针对类型不匹配问题,将
runtime.wrapPrimitive(value) ?? value as $Value?改为runtime.wrapPrimitive(value) as $Value? ?? value as $Value?,统一两边的类型为$Value?。
- 打开报错的
内容的提问来源于stack exchange,提问作者Hannon qaoud
相关产品推荐
相关产品推荐

