Я пытаюсь разработать приложение-калькулятор свободного падения, которое пытается сохранить вычисления в базе данных sembast. Но, по-видимому, он даже не получает данные из-за того, что застрял в состоянии загрузки.
Я с нетерпением жду ваших ответов.
Следующие файлы представляют собой блок bloc_pattern, а также модель.
class FreeFallTable {
static const double g = 9.81;
int id;
bool isNotRelevant;
String timeStamp;
double s0, v0, timeInterval, m, cw, a, rho;
List<double> times;
List<double> v1s;
List<double> distances;
List<double> fls;
List<double> fress;
List<double> accelerations;
List<double> deltavs;
List<double> v2s;
FreeFallTable(double s0, double v0, double timeInterval, double m, double cw, double a, double rho) {
this.isNotRelevant = false;
this.timeStamp = DateTime.now().millisecondsSinceEpoch.toString();
this.s0 = s0;
this.v0 = v0;
this.timeInterval = timeInterval;
this.m = m;
this.cw = cw;
this.a = a;
this.rho = rho;
this.times = new List<double>();
this.v1s = new List<double>();
this.distances = new List<double>();
this.fls = new List<double>();
this.fress = new List<double>();
this.accelerations = new List<double>();
this.deltavs = new List<double>();
this.v2s = new List<double>();
//Initialize Variables
this._initVariables();
}
void _initVariables() {
double t = 0;
double v_1 = g * t + v0;
double v_2;
double s_1 = 0.5 * g * pow(t, 2) + v0 * t;
double s_2;
for(int i = 0; i < 100; i++) {
double fl = 0.5 * cw * rho * a * pow(v_1, 2);
double fres = m * g - 0.5 * cw * rho * a * pow(v_1, 2);
double acceleration = fres / m;
double deltav = acceleration * timeInterval;
v_2 = v_1 + acceleration * timeInterval;
times.add(t);
v1s.add(v_1);
distances.add(s_1);
fls.add(fl);
fress.add(fres);
accelerations.add(acceleration);
deltavs.add(deltav);
v2s.add(v_2);
s_2 = s_1 + ((v_1 + v_2) / 2) * timeInterval;
s_1 = s_2;
v_1 = v_2;
t += timeInterval;
}
}
Map<String, dynamic> toJSON() {
return {
"isNotRelevant": isNotRelevant,
"timeStamp": timeStamp,
"s0": s0,
"v0": v0,
"timeInterval": timeInterval,
"m": m,
"cw": cw,
"a": a,
"rho": rho,
"times": times,
"v1s": v1s,
"distances": distances,
"fls": fls,
"fress": fress,
"accelerations": accelerations,
"deltavs": deltavs,
"v2s": v2s
};
}
FreeFallTable.alreadyInit({
@required this.isNotRelevant,
@required this.timeStamp,
@required this.s0,
@required this.v0,
@required this.timeInterval,
@required this.m,
@required this.cw,
@required this.a,
@required this.rho,
@required this.times,
@required this.v1s,
@required this.distances,
@required this.fls,
@required this.fress,
@required this.accelerations,
@required this.deltavs,
@required this.v2s
});
factory FreeFallTable.fromJSON(Map<String, dynamic> json) {
var timesFromJson = json['times'];
List<double> timesList = new List<double>.from(timesFromJson);
var v1sFromJson = json['v1s'];
List<double> v1sList = new List<double>.from(v1sFromJson);
var distancesFromJson = json['distances'];
List<double> distancesList = new List<double>.from(distancesFromJson);
var flsFromJson = json['fls'];
List<double> flsList = new List<double>.from(flsFromJson);
var fressFromJson = json['fress'];
List<double> fressList = new List<double>.from(fressFromJson);
var accelerationsFromJson = json['accelerations'];
List<double> accelerationsList = new List<double>.from(accelerationsFromJson);
var deltavsFromJson = json['deltavs'];
List<double> deltavsList = new List<double>.from(deltavsFromJson);
var v2sFromJson = json['v2s'];
List<double> v2sList = new List<double>.from(v2sFromJson);
return new FreeFallTable.alreadyInit(
isNotRelevant: json['isNotRelevant'],
timeStamp: json['timeStamp'],
s0: json['s0'],
v0: json['v0'],
timeInterval: json['timeInterval'],
m: json['m'],
cw: json['cw'],
a: json['a'],
rho: json['rho'],
times: timesList,
v1s: v1sList,
distances: distancesList,
fls: flsList,
fress: fressList,
accelerations: accelerationsList,
deltavs: deltavsList,
v2s: v2sList
);
}
}
Следующий файл описывает каждое событие
abstract class FreeFallEvents extends Equatable {
FreeFallEvents([List props = const []]) : super(props);
}
class AddFreeFallEvent extends FreeFallEvents {
//required for calc
//double s_0, double v_0, double time_interval, double m, double c_w, double a, double rho
final double s0;
final double v0;
final double timeInterval;
final double m;
final double cw;
final double a;
final double rho;
AddFreeFallEvent(this.s0, this.v0, this.timeInterval, this.m, this.cw, this.a, this.rho) : super([s0, v0, timeInterval, m, cw, a, rho]);
}
class DeleteFreeFallEvent extends FreeFallEvents {
final FreeFallTable table;
DeleteFreeFallEvent(this.table) : super([table]);
}
class UpdateFreeFallEvent extends FreeFallEvents {
final FreeFallTable table;
UpdateFreeFallEvent(this.table) : super([table]);
}
class QueryFreeFallEvent extends FreeFallEvents {
}
class FreeFallPageStartedEvent extends FreeFallEvents {
}
Следующий файл описывает каждое состояние
abstract class FreeFallStates extends Equatable {
FreeFallStates([List props = const []]) : super(props);
}
class LoadingFreeFallState extends FreeFallStates {
}
class EmptyFreeFallState extends FreeFallStates {
}
class LoadedFreeFallState extends FreeFallStates {
List<FreeFallTable> list;
LoadedFreeFallState(this.list) : super([list]);
}
следующий файл - события обработки файла blo c и уступающие состояния
class FreeFallBloc extends Bloc<FreeFallEvents, FreeFallStates> {
final FreeFallTableDoa _freeFallTableDoa;
int tableCount;
int isNotRelevantCount;
FreeFallBloc(this._freeFallTableDoa);
@override
FreeFallStates get initialState => LoadingFreeFallState();
@override
Stream<FreeFallStates> mapEventToState(FreeFallEvents event) async* {
if(event is AddFreeFallEvent) {
//create new freefalltable
FreeFallTable table = new FreeFallTable(event.s0, event.v0, event.timeInterval, event.m, event.cw, event.a, event.rho);
//Insert table in db
await _freeFallTableDoa.insert(table);
//query db to update ui
dispatch(new QueryFreeFallEvent());
} else if(event is UpdateFreeFallEvent) {
//update table
await _freeFallTableDoa.update(event.table);
//query db to update ui
dispatch(new QueryFreeFallEvent());
} else if(event is DeleteFreeFallEvent) {
//delete table
await _freeFallTableDoa.delete(event.table);
//query db to update ui
dispatch(new QueryFreeFallEvent());
} else if(event is QueryFreeFallEvent) {
print("Query");
//get all tables
final tableList = await _freeFallTableDoa.getAllSortedByTimeStamp();
isNotRelevantCount = tableList.where((f) => f.isNotRelevant).length;
if(tableList.isEmpty) {
//if empty yield empty state
yield(EmptyFreeFallState());
} else {
tableCount = tableList.length;
//yield loaded state onto the stream with the list
yield(LoadedFreeFallState(tableList));
}
}
}
}