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!!!