Issue
the problem is that I have two cars when I fetch the data with getting request the existing cars that work give me the information that I need but when I access the car that has null information the flutter gives me an error this is my code
class Vehicle {
Vehicle({
@required this.id,
@required this.name,
@required this.status,
@required this.position,
@required this.vehicleId,
@required this.driver,
@required this.motionState,
@required this.motionTime,
@required this.immobileFeature,
@required this.geofenceIds,
@required this.deviceState,
});
final int? id;
final String? name;
final String? status;
final Position? position;
final int? vehicleId;
final Driver? driver;
final String? motionState;
final String? motionTime;
final bool? immobileFeature;
final List<dynamic>? geofenceIds;
final DeviceState? deviceState;
factory Vehicle.fromJson(Map<String, dynamic> json) => Vehicle(
id: json["id"],
name: json["name"],
status: json["status"],
position: json["position"] == null
? null
: Position.fromJson(json["position"]),
vehicleId: json["vehicleId"],
driver: json["driver"] == null ? null : Driver.fromJson(json["driver"]),
motionState: json["motionState"],
motionTime: json["motionTime"] == null ? null : json["motionTime"],
immobileFeature: json["immobileFeature"],
geofenceIds: json["geofenceIds"] == null
? null
: List<dynamic>.from(json["geofenceIds"].map((x) => x)),
deviceState: json["deviceState"] == null
? null
: DeviceState.fromJson(json["deviceState"]),
);
}
**this is the main file I don't know if i need to put a variable for each null item or where the problem is **
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Future<List<Vehicle>> futureVehicle;
@override
void initState() {
super.initState();
futureVehicle = fetchVehicle();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
),
home: Scaffold(
),
body: FutureBuilder<List<Vehicle>>(
future: futureVehicle,
builder: (context, snapshot) {
if (snapshot.hasData) {
return (ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index) => GestureDetector(
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
fullscreenDialog: true,
// ignore: prefer_const_constructors
builder: (context) => carscreen(snapshot.data[index]),
),
),
children: [
Text(
"${snapshot.data[index].name}",
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 10),
Text("${snapshot.data[index].id}"),
SizedBox(height: 10),
Text("${snapshot.data[index].name}"),
SizedBox(height: 10),
Text("${snapshot.data[index].vehicleId}"),
SizedBox(height: 10),
Text("${snapshot.data[index].motionState}"),
],
),
),
),
));
} else {
return Center(child: CircularProgressIndicator());
}
},
),
),
);
}
}
Solution
factory Vehicle.fromJson(Map<String, dynamic> json) => Vehicle(
id: json["id"],
name: json["name"],
status: json["status"],
position: json["position"] == null
? new Position(
fixTime: '',
speed: 0,
bearing: 0,
address: 'null',
latlng: [],
attributes: new Attributes(
alarm: '',
immobile: '',
odometer: 0,
fuelUsed: 0,
avgFuelUsed: 0,
ignition: false,
coolantTemp: 0,
power: 0,
battery: 0))
: Position.fromJson(json["position"]),
vehicleId: json["vehicleId"],
driver: json["driver"] == null
? new Driver(name: 'null', phone: 'null')
: Driver.fromJson(json["driver"]),
motionState: json["motionState"],
motionTime: json["motionTime"] == null ? null : json["motionTime"],
immobileFeature: json["immobileFeature"],
geofenceIds: json["geofenceIds"] == null
? null
: List<dynamic>.from(json["geofenceIds"].map((x) => x)),
deviceState: json["deviceState"] == null
? new DeviceState(parkingAddress: 'null', tracks: [], traveldistance: 0, travelingDuration: 00)
: DeviceState.fromJson(json["deviceState"]),
);
}
Answered By - BM Chiheb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.