DART未经手的例外:Nosuchmethoderror:无顶级Getter' xx'声明。不得运行运行主DART入口点

发布于 2025-01-23 15:06:14 字数 4837 浏览 3 评论 0 原文

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

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.swift

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()

    }
    
}

(2) TestController.swift

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()
       
    }
}

(3) AppDelegate.swift

import UIKit
import Flutter

@UIApplicationMain
class AppDelegate: FlutterAppDelegate {
    
    //MARK: Flutter Engines
    lazy var flutterEngines = FlutterEngineGroup(name: "native_flutter_engine_group", project: nil)


(4) enter flutter screen

let testVC = TestController(withEntrypoint: "test")
navigationController?.pushViewController(testVC, animated: true)

...then run the app on an iOS device.

Expected behavior

Expect test() to be run as the program's entry-point, yielding a normal flutter screen.

Actual behavior

The app runs well in debug mode, but fails in release mode with the following error:

[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.

My try

When I removed the 'shared_preferences: ^2.0.13' package from the 'pubspec.yaml', it worked well in both debug and release mode.
Then I also tried other packages(local-storage related and others), I found that once I used packages about local storage (e.g. shared_preferences、cached_network_image ...), the problem showed up definitely, and when I removed them, everything was ok. (All the local- storage related packages, like the shared_preferences, works well in pure Flutter project in both debug and release mode, but not worked in iOS x Flutter project in release mode.)

PS:When we enter flutter module by the following

void main() => runApp(const MyApp());

instead of

@pragma('vm:entry-point')
void test() => runApp(const MyApp());

No Problem!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文