【问题标题】:Is there any way we can inject provider dependancy to normal class in flutter?有什么方法可以将提供者依赖注入到 Flutter 中的普通类中?
【发布时间】:2020-08-10 21:32:44
【问题描述】:

我有一个普通的 dart 类,我想在其中提供两个提供程序依赖项。

所以我可以通过我的应用程序访问该类。我可以从小部件的 build 方法传递该依赖项,然后我可以使用该类,但我不想这样做,例如如果我使用该类 100 次,我必须传递该依赖项 100 次。

我还想在 Flutter 的每个生命周期中使用这个类,因为它会为应用程序生成不同类型的事件。

我还想在依赖项准备好和用户打开应用程序时只初始化一次 genrateUserProerties() 方法。

这是在任何提供程序初始化之前初始化的,并不总是在我们有可用上下文的方法中使用。

我需要一种方法来提供这种依赖关系,我们只能初始化一次 genrateUserProerties()

User _user;BrandCofiguration _activeBrand; 我需要这两个在准备好后通过这里。

当我收到来自服务器的有效响应时,

User _user;BrandCofiguration _activeBrand; 都来自两个不同的提供商。

class FireBaseAnalyticsBase {
  static FirebaseAnalytics _analytics;
  static FirebaseAnalyticsObserver _observer;
  **User _user;**
  BuildContext _context;
  **BrandCofiguration _activeBrand;**
  int _seconds;
  Stopwatch _stopwatch;
  String _eventName;
  Map<String, dynamic> _userProperties = {};
  bool _isTimeTrackEvent;


  FireBaseAnalyticsBase(BuildContext context, UserProvider userProvider,
      BrandSelectionProvider brandSelectionProvider) { 
    this._context = context;
    _analytics = FirebaseAnalytics();
    _observer = FirebaseAnalyticsObserver(analytics: _analytics);
    _activeBrand = brandSelectionProvider.activeBrand;
    _user = userProvider.authenticatedUser;
    if (_user != null) {
      genrateUserProerties();
    }
  }

  void startFirebaseEventWithoutTime(String eventName) {
    this._eventName = eventName;
    _isTimeTrackEvent = false;
    logFirebaseEvent();
  }

  void startFireBaseEventWithTime(String eventName) {
    _stopwatch = Stopwatch();
    _stopwatch.start();
    _isTimeTrackEvent = true;
    _eventName = eventName;
  }

  void stopFireBaseTimeEvent() {
    _stopwatch.stop();
    _seconds = (_stopwatch.elapsedMilliseconds / 1000) as int;
    _stopwatch.reset();
    logFirebaseEvent();
  }

  Future<void> logFirebaseEvent() async {
    if (_isTimeTrackEvent) {
      _userProperties
          .addAll({FirebaseAnalyticsEnum.time_spent.value: _seconds});
    }
    print("firebase test");
    await _analytics.logEvent(
      name: _eventName,
      parameters: _userProperties,
    );
  }

  Future<void> genrateUserProerties() async {
    print("firebase properties initilize");
    var _packageInfo = await PackageInfo.fromPlatform();
    _userProperties = {
      FirebaseAnalyticsEnum.user_id.value: _user.id.toString(),
      FirebaseAnalyticsEnum.platform.value: Platform.operatingSystem,
      FirebaseAnalyticsEnum.device_language.value:
          Localizations.localeOf(_context).languageCode,
      FirebaseAnalyticsEnum.application.value: _packageInfo.appName,
      FirebaseAnalyticsEnum.current_api.value: Config.CURRENT_API,
      FirebaseAnalyticsEnum.device_type.value: _user.id.toString(),
      FirebaseAnalyticsEnum.app_version.value: Config.CURRENT_VERSION,
      FirebaseAnalyticsEnum.is_admin.value: _user.isAdmin,
      FirebaseAnalyticsEnum.is_educator.value: _user.educator,
      FirebaseAnalyticsEnum.is_brand_ambassador.value: _user.brandAmbassador,
      FirebaseAnalyticsEnum.salon_role.value: _user.salongroup,
      FirebaseAnalyticsEnum.brand.value: _activeBrand.brandName,
      FirebaseAnalyticsEnum.school_role.value: _user.schoolgroup,
    };
  }
}

【问题讨论】:

    标签: flutter dart flutter-dependencies provider bloc


    【解决方案1】:

    我认为您应该使用Singleton 模式,因为这些类会消耗大量资源。 示例:

    static FireBaseAnalyticsBase _instance;
    
    static FireBaseAnalyticsBase getInstance(BuildContext context, UserProvider userProvider,
          BrandSelectionProvider brandSelectionProvider){
        if(_instance == null){
            _instance = FireBaseAnalyticsBase(context,userProvider,brandSelectionProvider);
        }
        return _instance;
    }
    

    或者如果你通过 BuildContext 你可以得到 Provider Provider.of&lt;&gt;(context) 在 FireBaseAnalyticsBase 的构造函数中

    【讨论】:

    • 这是在任何提供程序初始化之前初始化的,它并不总是在我们有可用上下文的方法中使用。
    • 因此请考虑从一开始就使用 Provider(create: FireBaseAnalyticsBase()) 或 multiProvider 提供它
    猜你喜欢
    • 2021-09-23
    • 2023-01-22
    • 2021-04-23
    • 1970-01-01
    • 2018-08-21
    • 2021-02-18
    • 2020-01-30
    • 2012-06-11
    • 1970-01-01
    相关资源
    最近更新 更多