DART未经手的例外:Nosuchmethoderror:无顶级Getter' xx'声明。不得运行运行主DART入口点
Steps to reproduce
NO.1 Create the following Dart program:
(1) main.dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
@pragma('vm:entry-point')
void test() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: const Center(
child: Text('Flutter Demo'),
),
),
);
}
}
(2) pubspec.yaml
version: 1.0.0+1
environment:
sdk: ">=2.16.2 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
shared_preferences: ^2.0.13
(3) flutter doctor
/Users/dqs/Documents/CompanyProjects/Flutter/flutter/bin/flutter doctor --verbose
[✓] Flutter (Channel stable, 2.10.4, on macOS 12.0.1 21A559 darwin-x64, locale zh-Hans-CN)
• Flutter version 2.10.4 at /Users/dqs/Documents/CompanyProjects/Flutter/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision c860cba910 (4 weeks ago), 2022-03-25 00:23:12 -0500
• Engine revision 57d3bac3dd
• Dart version 2.16.2
• DevTools version 2.9.2
• Pub download mirror https://pub.flutter-io.cn
• Flutter download mirror https://storage.flutter-io.cn
NO.2 ~ Create the following iOS program:
(1) BaseFlutterController 。
import UIKit
import Flutter
class BaseFlutterController: FlutterViewController {
override init(engine: FlutterEngine, nibName: String?, bundle nibBundle: Bundle?) {
super.init(engine: engine, nibName: nibName, bundle: nibBundle)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
import UIKit
import FlutterPluginRegistrant
class TestController: BaseFlutterController {
init(withEntrypoint entryPoint: String?) {
let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
let newEngine = appDelegate.flutterEngines.makeEngine(withEntrypoint: entryPoint, libraryURI: nil)
// register third-party packages
GeneratedPluginRegistrant.register(with: newEngine)
super.init(engine: newEngine, nibName: nil, bundle: nil)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
import UIKit
import Flutter
@UIApplicationMain
class AppDelegate: FlutterAppDelegate {
//MARK: Flutter Engines
lazy var flutterEngines = FlutterEngineGroup(name: "native_flutter_engine_group", project: nil)
let testVC = TestController(withEntrypoint: "test")
navigationController?.pushViewController(testVC, animated: true)
预期的行为
期望测试()作为程序的入口点运行,并产生正常的颤音屏幕。
实际行为
该应用程序在调试模式下运行良好,但在版本中失败模式,并具有以下错误:
[768:219965] [VERBOSE-2:shell.cc(93)] Dart Unhandled Exception: NoSuchMethodError: No top-level getter 'test' declared.
Receiver: top-level
Tried calling: test, stack trace: #0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:222)
[768:219965] [VERBOSE-2:dart_isolate.cc(681)] Could not resolve main entrypoint function.
[VERBOSE-2:dart_isolate.cc(165)] Could not run the run main Dart entrypoint.
[768:219965] [VERBOSE-2:runtime_controller.cc(381)] Could not create root isolate.
[768:219965] [VERBOSE-2:shell.cc(580)] Could not launch engine with configuration.
我的尝试
当我从“ pubspec.yaml”中删除“ shared_preferences: ^2.0.13”软件包时,它在 debug 和 repartion 模式下均效果很好。 然后我还尝试了其他软件包(Local-Storage与其他软件包和其他软件包),我发现一旦使用了有关本地存储的软件包(例如shared_preferences,cached_network_image ...),问题肯定会出现,当我删除它们时,一切都可以了。 。 (所有本地存储相关的软件包,例如共享_preference,在 pure flutter project中都很好地工作 debug 和版本模式,但是版本模式中不工作。
>在 ios x
void main() => runApp(const MyApp());
@pragma('vm:entry-point')
void test() => runApp(const MyApp());
Flutter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论