我正在尝试使用Bloc模式获取sqlite表中一列的总和。
debt_bloc.dart
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, BackgroundService.class);
startService(intent);
}
debt_dao.dart
private Handler mainHandler;
private RenderersFactory renderersFactory;
private BandwidthMeter bandwidthMeter;
private LoadControl loadControl;
private DataSource.Factory dataSourceFactory;
private ExtractorsFactory extractorsFactory;
private MediaSource mediaSource;
private TrackSelection.Factory trackSelectionFactory;
private SimpleExoPlayer player;
private final String streamUrl = "http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-einws";
private TrackSelector trackSelector;
@Override
public void onCreate() {
super.onCreate();
startPlayer();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startPlayer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void startPlayer(){
renderersFactory = new DefaultRenderersFactory(getApplicationContext());
bandwidthMeter = new DefaultBandwidthMeter();
trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
loadControl = new DefaultLoadControl();
player = ExoPlayerFactory.newSimpleInstance(this, renderersFactory, trackSelector, loadControl);
player.addListener(this);
dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), "ExoplayerDemo");
extractorsFactory = new DefaultExtractorsFactory();
mainHandler = new Handler();
mediaSource = new ExtractorMediaSource(Uri.parse(streamUrl),
dataSourceFactory,
extractorsFactory,
mainHandler,
null);
player.prepare(mediaSource);
}
debt_repositary.dart
getTotalAmount() async {
return await _debtRepository.getTotalAmt();
}
当我尝试如下打印时
Future<int> getTotalAmount() async {
final db = await dbProvider.database;
var result = await db.rawQuery("SELECT SUM(amount) FROM $debtDetailsTable");
int value = result[0]["SUM(amount)"];
return value;
}
添加新数据时输出未更新。但是,如果返回主屏幕并转到各自的屏幕,则该值正在更新。
请以正确的方式引导我。预先感谢
答案 0 :(得分:1)
在打印之前先等待您的方法。
var total = await debtBloc.getTotalAmount(); // await here
print(total); // should now print some int value