条件包导入破坏代码 Flutter
Conditional package import breaks the code Flutter
在我的应用程序中,我正在使用 sembast
数据库,我正在尝试使 AppDatabase
单例 class 适应平台并在应用程序是时使用 sambast_web
运行 在网络上。
我正在尝试使用与用户位置相同的模式,所以我创建了:
- 具有 getter 方法的存根。
- 一个
abstract class AppDatabase
在导入存根时为设备和 Web 包进行条件包导入,并在 class 工厂方法中 returns 存根 getter方法。
- A
class AppDatabaseDevice
和 class AppDatabaseWeb
都实现了 AppDatabase
.
当 运行 应用面团出现错误时:
Compiler message:
lib/fixit_shop_app/database/app_database_switcher.dart:50:28: Error: Method not found: 'getAppDatabase'.
factory AppDatabase() => getAppDatabase();
^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
Failed to build bundle.
Error launching application on iPad (6th generation).
现在,这很奇怪,因为在为抽象 class 工厂编写代码时,它发现 getAppDatabase()
没有抛出任何错误。
在修改了一些代码后,我确定了存根导入的条件导入问题。
如果我在没有条件导入的情况下将其导入为
import 'package:fixit_shop_flutter/fixit_shop_app/database/app_database_stub.dart';
然后我没有收到任何错误,但我又需要条件导入..
你能找出为什么条件导入对这个 class 失败而对另一个有效吗?
一如既往,非常感谢您的宝贵时间和帮助。
这些是 AppDatabase 方法:
存根:
import 'app_database_switcher.dart';
AppDatabase getAppDatabase() => throw UnsupportedError(
'Cant get AppDatabase if not loading the right package');
摘要class:
import 'dart:async';
import 'package:sembast/sembast.dart';
import 'app_database_stub.dart'
if (dart.library.io) 'package:sembast/sembast.dart'
if (dart.library.js) 'package:sembast_web/sembast_web.dart';
abstract class AppDatabase {
// Singleton instance
static final AppDatabase _singleton;
// Singleton accessor
static AppDatabase get instance => _singleton;
// Completer is used for transforming synchronous code into asynchronous code.
Completer<Database> _dbOpenCompleter;
// A private constructor. Allows us to create instances of AppDatabase
// only from within the AppDatabase class itself.
// Sembast database object
Database _database;
// Database object accessor
Future<Database> get database async {
// // If completer is null, AppDatabaseClass is newly instantiated, so database is not yet opened
return _dbOpenCompleter.future;
}
factory AppDatabase() => getAppDatabase();
}
这是用户定位方法:
存根:
import 'package:fixit_shop_flutter/fixit_shop_app/platform_user_location/user_location_switcher.dart';
UserLocation getUserLocation() =>
throw UnsupportedError('user_location_stub error');
摘要class:
import 'package:fixit_shop_flutter/fixit_shop_app/platform_user_location/user_location_stub.dart' // Version which just throws UnsupportedError
if (dart.library.io) "package:fixit_shop_flutter/fixit_shop_app/platform_user_location/user_location_device.dart"
if (dart.library.js) "package:fixit_shop_flutter/fixit_shop_app/platform_user_location/user_location_web.dart";
abstract class UserLocation {
// methods to be used
Future<Map<String, dynamic>> getPosition() async {
Future<Map<String, dynamic>> position;
return position;
}
factory UserLocation() => getUserLocation();
}
发现错误。事实上,这是有条件的进口。我正在导入各种 sembast 包而不是设备和网络 class 实现..
import 'package:fixit_shop_flutter/fixit_shop_app/database/app_database_stub.dart'
if (dart.library.io) 'package:fixit_shop_flutter/fixit_shop_app/database/app_database_device.dart'
if (dart.library.js) 'package:fixit_shop_flutter/fixit_shop_app/database/app_database_web.dart';
在我的应用程序中,我正在使用 sembast
数据库,我正在尝试使 AppDatabase
单例 class 适应平台并在应用程序是时使用 sambast_web
运行 在网络上。
我正在尝试使用与用户位置相同的模式,所以我创建了:
- 具有 getter 方法的存根。
- 一个
abstract class AppDatabase
在导入存根时为设备和 Web 包进行条件包导入,并在 class 工厂方法中 returns 存根 getter方法。 - A
class AppDatabaseDevice
和class AppDatabaseWeb
都实现了AppDatabase
.
当 运行 应用面团出现错误时:
Compiler message:
lib/fixit_shop_app/database/app_database_switcher.dart:50:28: Error: Method not found: 'getAppDatabase'.
factory AppDatabase() => getAppDatabase();
^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
Failed to build bundle.
Error launching application on iPad (6th generation).
现在,这很奇怪,因为在为抽象 class 工厂编写代码时,它发现 getAppDatabase()
没有抛出任何错误。
在修改了一些代码后,我确定了存根导入的条件导入问题。
如果我在没有条件导入的情况下将其导入为
import 'package:fixit_shop_flutter/fixit_shop_app/database/app_database_stub.dart';
然后我没有收到任何错误,但我又需要条件导入..
你能找出为什么条件导入对这个 class 失败而对另一个有效吗?
一如既往,非常感谢您的宝贵时间和帮助。 这些是 AppDatabase 方法:
存根:
import 'app_database_switcher.dart';
AppDatabase getAppDatabase() => throw UnsupportedError(
'Cant get AppDatabase if not loading the right package');
摘要class:
import 'dart:async';
import 'package:sembast/sembast.dart';
import 'app_database_stub.dart'
if (dart.library.io) 'package:sembast/sembast.dart'
if (dart.library.js) 'package:sembast_web/sembast_web.dart';
abstract class AppDatabase {
// Singleton instance
static final AppDatabase _singleton;
// Singleton accessor
static AppDatabase get instance => _singleton;
// Completer is used for transforming synchronous code into asynchronous code.
Completer<Database> _dbOpenCompleter;
// A private constructor. Allows us to create instances of AppDatabase
// only from within the AppDatabase class itself.
// Sembast database object
Database _database;
// Database object accessor
Future<Database> get database async {
// // If completer is null, AppDatabaseClass is newly instantiated, so database is not yet opened
return _dbOpenCompleter.future;
}
factory AppDatabase() => getAppDatabase();
}
这是用户定位方法:
存根:
import 'package:fixit_shop_flutter/fixit_shop_app/platform_user_location/user_location_switcher.dart';
UserLocation getUserLocation() =>
throw UnsupportedError('user_location_stub error');
摘要class:
import 'package:fixit_shop_flutter/fixit_shop_app/platform_user_location/user_location_stub.dart' // Version which just throws UnsupportedError
if (dart.library.io) "package:fixit_shop_flutter/fixit_shop_app/platform_user_location/user_location_device.dart"
if (dart.library.js) "package:fixit_shop_flutter/fixit_shop_app/platform_user_location/user_location_web.dart";
abstract class UserLocation {
// methods to be used
Future<Map<String, dynamic>> getPosition() async {
Future<Map<String, dynamic>> position;
return position;
}
factory UserLocation() => getUserLocation();
}
发现错误。事实上,这是有条件的进口。我正在导入各种 sembast 包而不是设备和网络 class 实现..
import 'package:fixit_shop_flutter/fixit_shop_app/database/app_database_stub.dart'
if (dart.library.io) 'package:fixit_shop_flutter/fixit_shop_app/database/app_database_device.dart'
if (dart.library.js) 'package:fixit_shop_flutter/fixit_shop_app/database/app_database_web.dart';