2

我在带有 Riverpod 的 Flutter 2.5 上使用 ObjectBox。目标是允许快速缓存来自 API 的大量数据。

如果远程数据和盒子里的数据有差异,那么我们更新。不再拥有互联网的人仍然拥有良好的数据。

有没有人设法在使用 Riverpod 的 Flutter Clean 架构中实例化并最好地使用 Objectbox?我在这里这里发现了几个相关的问题。但我目前似乎无法得到任何满意的东西......

如果您对此有任何建议。我考虑了 Objectbox 在他们的商店创建示例中所做的更新。

错误总是相同的,要么 _box 或 store 没有实例化,要么得到 null。我会在研究时更新我的​​帖子。

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: LateInitializationError: Field '_box@59089156' has not been initialized.

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: LateInitializationError: Field 'store' has not been initialized.

------编辑 2021 年 10 月 21 日-----

好的,感谢同事的帮助和意见。我更新我的代码以匹配。不再有存储或盒子实例化问题。

我的代码

abstract class LocalDataSourceRepository<T> {
  Future<Coin?> getCoinById(dynamic id);
  Future<void> addCoin(T object);
}

远程/本地管理在此实现中,使用 getRemoteDataCoin 从 API 检索完整货币并将其转换为实体

class CoinRepositoryImpl extends LocalDataSourceRepository {

  final CoinRemoteApi _remoteApi;
  final ObjectBoxDatabase _objectBoxDatabase;

  CoinRepositoryImpl(
      this._remoteApi,
      this._objectBoxDatabase,
  );

  @override
  Future<Coin?> getCoinById(id) async {
    final _box = _objectBoxDatabase.store.box<Coin>();
    final Query<Coin> query = (_boxCoin.query(Coin_.coinId.equals(id))).build();

    if (query.findUnique() == null) {
      final coin = await getRemoteDataCoin(id);
      addCoin(coin);
      return query.findUnique();
    } else {
      return query.findUnique();
    }
  }

  @override
  Future<void> addCoin(object) async {
    final _box = _objectBoxDatabase.store.box<Coin>();
    _box.put(object);
  }

  Future<Coin> getRemoteDataCoin(selectedCoin) async {
    return _remoteApi.getCoinById(
        selectedCoin,
        const CoinRequest(
            localization: 'false',
            tickers: false,
            marketData: false,
            communityData: true,
            developerData: false,
            sparkline: false
        )
    ).then(
          (value) => value.toEntity(),
    );
  }
}
class ObjectBoxDatabase {
  late final Store store;
  late final Box<Coin> _box;

  ObjectBoxDatabase._create(this.store) {
     _box = Box<Coin>(store);
  }

  static Future<ObjectBoxDatabase> create() async {
    final store = await openStore();
    return ObjectBoxDatabase._create(store);
  }
}

我的 Riverpod 供应商。

final localCoinDatabaseProvider = Provider<ObjectBoxDatabase>((ref) => throw UnimplementedError());
final remoteCoinApiProvider = Provider<CoinRemoteApi>((ref) => CoinRemoteApi());

final coinRepositoryProvider = Provider<LocalDataSourceRepository>((ref) => CoinRepositoryImpl(
    ref.read(remoteCoinApiProvider),
    ref.read(localCoinDatabaseProvider)
));
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final objectBox = await ObjectBoxDatabase.create();
  runApp(
      ProviderScope(
          overrides: [
            localCoinDatabaseProvider.overrideWithValue(objectBox)
          ],
          child: const MyApp()
      )
  );
}
4

1 回答 1

2

由于您使用未来的 .create 方法获取 ObjectBoxDatabase,因此您可以使用 FutureProvider 或覆盖

未来提供者:

final localCoinDatabaseProvider = FutureProvider<ObjectBoxDatabase>((ref) => ObjectBoxDatabase.create());

https://pub.dev/documentation/riverpod/latest/riverpod/FutureProvider-class.html

覆盖:

final localCoinDatabaseProvider = Provider<ObjectBoxDatabase>((ref) => throw UnimplementedError());


Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final objectBox = await ObjectBoxDatabase.create();
  runApp(
      const ProviderScope(
        overrides: [localCoinDatabaseProvider.overrideWithValue(objectBox)]
        child: MyApp()
      )
  );
}
于 2021-10-20T11:20:16.817 回答