Skip to content

Instantly share code, notes, and snippets.

@anoochit
Created June 22, 2022 03:25
Show Gist options
  • Select an option

  • Save anoochit/ac7ee048edcdbe94fc35ca7dda717825 to your computer and use it in GitHub Desktop.

Select an option

Save anoochit/ac7ee048edcdbe94fc35ca7dda717825 to your computer and use it in GitHub Desktop.
ev station model with geoflutterfire
// To parse this JSON data, do
//
// final station = stationFromJson(jsonString);
//import 'package:meta/meta.dart';
import 'dart:convert';
import 'dart:developer';
import 'package:evstation/const.dart';
import 'package:flutter/services.dart';
import 'package:geoflutterfire/geoflutterfire.dart';
List<Station> stationFromJson(String str) => List<Station>.from(json.decode(str).map((x) => Station.fromJson(x)));
String stationToJson(List<Station> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Station {
Station({
required this.id,
required this.stationFavorite,
required this.name,
required this.address,
required this.description,
required this.latitude,
required this.longitude,
required this.status,
required this.statusId,
required this.remarkChargerType,
required this.distance,
required this.listConnector,
required this.canBeWalkIn,
required this.canBeReserve,
required this.supportAccessCode,
required this.isLowPriority,
required this.lowPriorityText,
});
String id;
bool stationFavorite;
String name;
String address;
String description;
double latitude;
double longitude;
String status;
String statusId;
String remarkChargerType;
String distance;
List<ListConnector> listConnector;
bool canBeWalkIn;
bool canBeReserve;
bool supportAccessCode;
bool isLowPriority;
String lowPriorityText;
factory Station.fromJson(Map<String, dynamic> json) => Station(
id: json["id"],
stationFavorite: json["station_favorite"],
name: json["name"],
address: json["address"],
description: json["description"],
latitude: json["latitude"].toDouble(),
longitude: json["longitude"].toDouble(),
status: json["status"],
statusId: json["status_id"],
remarkChargerType: json["remark_charger_type"],
distance: json["distance"],
listConnector: List<ListConnector>.from(json["list_connector"].map((x) => ListConnector.fromJson(x))),
canBeWalkIn: json["can_be_walk_in"],
canBeReserve: json["can_be_reserve"],
supportAccessCode: json["support_access_code"],
isLowPriority: json["is_low_priority"],
lowPriorityText: json["low_priority_text"],
);
Map<String, dynamic> toJson() => {
"id": id,
"station_favorite": stationFavorite,
"name": name,
"address": address,
"description": description,
"latitude": latitude,
"longitude": longitude,
"status": status,
"status_id": statusId,
"remark_charger_type": remarkChargerType,
"distance": distance,
"list_connector": List<dynamic>.from(listConnector.map((x) => x.toJson())),
"can_be_walk_in": canBeWalkIn,
"can_be_reserve": canBeReserve,
"support_access_code": supportAccessCode,
"is_low_priority": isLowPriority,
"low_priority_text": lowPriorityText,
};
}
class ListConnector {
ListConnector({
required this.connectorTypeId,
required this.connectorTypeName,
required this.chargerTypeName,
required this.totalAvailable,
required this.chargerAvailable,
required this.canBeWalkIn,
required this.canBeReserve,
required this.description,
required this.status,
required this.dischargeElectricity,
});
String connectorTypeId;
String connectorTypeName;
String chargerTypeName;
int totalAvailable;
bool chargerAvailable;
bool canBeWalkIn;
bool canBeReserve;
String description;
String status;
String dischargeElectricity;
factory ListConnector.fromJson(Map<String, dynamic> json) => ListConnector(
connectorTypeId: json["connector_type_id"],
connectorTypeName: json["connector_type_name"],
chargerTypeName: json["charger_type_name"],
totalAvailable: json["total_available"],
chargerAvailable: json["charger_available"],
canBeWalkIn: json["can_be_walk_in"],
canBeReserve: json["can_be_reserve"],
description: json["description"],
status: json["status"],
dischargeElectricity: json["discharge_electricity"],
);
Map<String, dynamic> toJson() => {
"connector_type_id": connectorTypeId,
"connector_type_name": connectorTypeName,
"charger_type_name": chargerTypeName,
"total_available": totalAvailable,
"charger_available": chargerAvailable,
"can_be_walk_in": canBeWalkIn,
"can_be_reserve": canBeReserve,
"description": description,
"status": status,
"discharge_electricity": dischargeElectricity,
};
}
///
addMocData() {
final geo = Geoflutterfire();
log("add mock data");
// load mockdata
rootBundle.loadString("assets/mockdata/station.json").then((json) {
//log(json);
final station = stationFromJson(json);
log('total station = ${station.length}');
station.forEach((item) {
// get geopoint
GeoFirePoint location = geo.point(
latitude: item.latitude,
longitude: item.longitude,
);
// add data to firestore
List<Map<String, String>> connector = [];
item.listConnector.forEach((element) {
connector.add({
"connector_type_id": element.connectorTypeId,
"connector_type_name": element.connectorTypeName,
"charger_type_name": element.chargerTypeName,
"discharge_electricity": element.dischargeElectricity,
});
});
firestore.collection("stations").doc(item.id).set({
"id": item.id,
"name": item.name,
"address": item.address,
"description": item.description,
"status_id": item.statusId,
"remark_charger_type": item.remarkChargerType,
"location": location.data,
"list_connector": connector,
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment