我用的是BLoCworkmanager图书馆.

实际上,代码运行得很好.但是,看起来像是气味代码.

我的问题是: 我必须为包装我的MainApp()部件的MultiRepositoryProvider初始化我的所有存储库.在这一点上,基本的东西.

void main() async {
 
  final RepositoryOne repoOne = RepositoryOne();
  final RepositoryTwo repoTwo = RepositoryTwo();

  runApp(
    MultiRepositoryProvider(
      providers: [
        RepositoryProvider(
          create: (context) => repoOne,
        ),
        RepositoryProvider(
          create: (context) => repoTwo,
        ),
      ],
      child: const MyApp(),
    ),
  );
}

但是,当我为我的工作经理声明一个工作时,我必须重新初始化其中的一些存储库.

@pragma('vm:entry-point')
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) async {
    switch (task) {
      case 'synchronize':
          final RepositoryOne repoOne = RepositoryOne();
          final RepositoryTwo repoTwo = RepositoryTwo();
        
            List<int> result = await repoOne.synchronize();
            if (result.isNotEmpty) {
             await repoTwo.doThings(result);
            }
        break;
    }
    return Future.value(true);
  });
}

我try 了几种方法在同一位置导出存储库初始化,但没有成功……

有没有人有一个初始化to not duplicate个存储库的 idea ?

Solution chosen

为了做到这一点,我go 掉了服务定位器库.

我做了什么:


late RepositoryOne repoOne;
late RepositoryTwo repoTwo;
late RepositoryThree repoThree;
late RepositoryFour repoFour;

void initDependenciesStuff() {
  repoOne = RepositoryOne();
  repoTwo = RepositoryTwo();
}

void initAnotherDependenciesStuff() {
  repoThree = RepositoryThree();
  repoFour = RepositoryFour();
}

void main() {
  initDependenciesStuff();
  initAnotherDependenciesStuff();

  runApp(
    MultiRepositoryProvider(
      providers: [
        RepositoryProvider(
          create: (context) => repoOne,
        ),
        RepositoryProvider(
          create: (context) => repoTwo,
        ),
        RepositoryProvider(
          create: (context) => repoThree,
        ),
        RepositoryProvider(
          create: (context) => repoFour,
        ),

      ],
      child: const MyApp(),
    ),
  );
}


@pragma('vm:entry-point')
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) async {
    switch (task) {
      case 'firstThing':
        initDependenciesStuff();

        List<int> stuff = repoOne.fetchStuff();
        await repoTwo.editStuffDatabase(stuff);
       
        return Future.value(true);
      case 'secondThing':
        initAnotherDependenciesStuff();

        List<int> updatedStuff = repoFour.updateStuff();
        await repoThree.editStuffDatabase(updatedStuff);

        return Future.value(true);
      case 'thirdThing':
        initDependenciesStuff();
        initAnotherDependenciesStuff();

        // Another stuffs to do

        return Future.value(true);
    }

    return Future.value(true);
  });
}

推荐答案

documentation的回调来看,Dispatcher是顶层函数.

因此,在不同的应用程序位置提供相同类实例的最佳方式是使用依赖注入或服务定位符,例如injectableget_it.

get_it英镑的价格大概是这样的.Main中的一些地方:

imp或t 'package:app.taletwist/injection_container.dart' as di;

  await di.init();
  runApp(..);

然后在INPULT_CONTAINER.DART中:

final sl = GetIt.instance;

Future<void> init() async {
  sl
    ..registerSingleton(Reposit或yOne())
    ..registerSingleton(Reposit或yTwo())
}

然后在任何你需要的地方:

  runApp(
    MultiReposit或yProvider(
      providers: [
        Reposit或yProvider(
          create: (context) => sl<Reposit或yOne>(),
        ),
        Reposit或yProvider(
          create: (context) => sl<Reposit或yTwo>(),
        ),
      ],
      child: const MyApp(),
    ),
  );

@pragma('vm:entry-point')
void callbackDispatcher() {
  W或kmanager().executeTask((task, inputData) async {
    switch (task) {
      case 'synchronize':
          final repoOne = sl<Reposit或yOne>();
          final repoTwo = sl<Reposit或yTwo>();
        
            List<int> result = await repoOne.synchronize();
            if (result.isNotEmpty) {
             await repoTwo.doThings(result);
            }
        break;
    }
    return Future.value(true);
  });
}

Flutter相关问答推荐

无法让Riverpod Stream发出新值

Ffltter:发布版本中的Path NotFoundException

如何在应用程序中使用Fflight显示表情包

从.gitignore中排除.fvm/fvm_config.json

Wired Firebase错误-[CLOUD_FIRESTORE/UNAvailable]该服务当前不可用

在Ffltter中,我已经使用了Ffltter_BLOC Cupit和Pusher,但当获得新数据时,UI没有更新

请求已在 flutter 中发送到 django 服务器,并且已返回响应,但条件仍然为 false

Dart 和 flutter 错误无法在您的 PATH 中找到 git

如何在flutter中将所有单词的第一个字母大写

Riverpod 2.3.6:AutoDisposeNotifier和ref.onDispose自动处理

如何计算 Firestore 集合中的文档数量?

像图像一样Flutter 自定义标签

如何从sibling ChildWidgetB 触发 ChildWidgetA 中的操作

Flutter - 展开图标无法正常工作

应用程序和应用程序内的 webview 之间的单点登录

type '({bool growable}) => List' 不是类型转换中类型 'List' 的子类型

在 flutter 中使用 StreamBuilder 在文本字段中键入时显示过滤记录

如何在dart中编写多个条件?

将容器扩展到安全区域 - Flutter

如何从 Flutter 中的列表中提取 JSON?