i want to get data from a list its working with the first 6 attributes but i wanted to add the "description", "assetsideid", "assetnum", "location" and "descriptionlongdescription" but it stopped showing data and i keep getting these errors (i got the code from a online json to dart) i get the error from the description till descriptionLongdescription "The argument type 'Affecteddate?' can't be assigned to the parameter type 'Affecteddate'" and thank you
import 'dart:convert';
Demandes azizFromJson(String str) => Demandes.fromJson(json.decode(str));
String azizToJson(Demandes data) => json.encode(data.toJson());
class Demandes {
Demandes({
required this.srMboSet,
});
SrMboSet srMboSet;
factory Demandes.fromJson(Map<String, dynamic> json) => Demandes(
srMboSet: SrMboSet.fromJson(json["SRMboSet"]),
);
Map<String, dynamic> toJson() => {
"SRMboSet": srMboSet.toJson(),
};
}
class SrMboSet {
SrMboSet({
required this.rsStart,
required this.rsCount,
required this.sr,
});
int rsStart;
int rsCount;
List<Sr> sr;
factory SrMboSet.fromJson(Map<String, dynamic> json) => SrMboSet(
rsStart: json["rsStart"],
rsCount: json["rsCount"],
sr: List<Sr>.from(json["SR"].map((x) => Sr.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"rsStart": rsStart,
"rsCount": rsCount,
"SR": List<dynamic>.from(sr.map((x) => x.toJson())),
};
}
class Sr {
Sr({
required this.rowstamp,
required this.attributes,
});
String rowstamp;
Attributes attributes;
factory Sr.fromJson(Map<String, dynamic> json) => Sr(
rowstamp: json["rowstamp"],
attributes: Attributes.fromJson(json["Attributes"]),
);
Map<String, dynamic> toJson() => {
"rowstamp": rowstamp,
"Attributes": attributes.toJson(),
};
}
class Attributes {
Attributes({
required this.ticketid,
required this.attributesClass,
required this.status,
required this.statusdate,
required this.reportedby,
required this.description,
required this.assetsiteid,
required this.assetnum,
required this.location,
required this.descriptionLongdescription,
});
Affecteddate ticketid;
Affecteddate attributesClass;
Affecteddate status;
Affecteddate statusdate;
Affecteddate reportedby;
Affecteddate description;
Affecteddate assetsiteid;
Affecteddate assetnum;
Affecteddate location;
Affecteddate descriptionLongdescription;
factory Attributes.fromJson(Map<String, dynamic> json) => Attributes(
ticketid: Affecteddate.fromJson(json["TICKETID"]),
attributesClass: Affecteddate.fromJson(json["CLASS"]),
status: Affecteddate.fromJson(json["STATUS"]),
statusdate: Affecteddate.fromJson(json["STATUSDATE"]),
reportedby: Affecteddate.fromJson(json["REPORTEDBY"]),
description: json["DESCRIPTION"] == null ? null : Affecteddate.fromJson(json["DESCRIPTION"]),
assetsiteid: json["ASSETSITEID"] == null ? null : Affecteddate.fromJson(json["ASSETSITEID"]),
assetnum: json["ASSETNUM"] == null ? null : Affecteddate.fromJson(json["ASSETNUM"]),
location: json["LOCATION"] == null ? null : Affecteddate.fromJson(json["LOCATION"]),
descriptionLongdescription: json["DESCRIPTION_LONGDESCRIPTION"] == null ? null : Affecteddate.fromJson(json["DESCRIPTION_LONGDESCRIPTION"]),
);
Map<String, dynamic> toJson() => {
"TICKETID": ticketid.toJson(),
"CLASS": attributesClass.toJson(),
"STATUS": status.toJson(),
"STATUSDATE": statusdate.toJson(),
"REPORTEDBY": reportedby.toJson(),
"DESCRIPTION": description == null ? null : description.toJson(),
"ASSETSITEID": assetsiteid == null ? null : assetsiteid.toJson(),
"ASSETNUM": assetnum == null ? null : assetnum.toJson(),
"LOCATION": location == null ? null : location.toJson(),
};
}
class Affecteddate {
Affecteddate({
required this.content,
});
String content;
factory Affecteddate.fromJson(Map<String, dynamic> json) => Affecteddate(
content: json["content"],
);
Map<String, dynamic> toJson() => {
"content": content,
};
}
and this is the code for importing the data its working fine before i add the descriptions
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:myapp2/Service_Request/SR.dart';
import 'package:myapp2/main.dart';
import 'package:myapp2/Service_Request/second.dart';
import '../Classes/demandes.dart';
import 'SR_details.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DataFromAPI(),
);
}
}
class DataFromAPI extends StatefulWidget {
@override
_DataFromAPIState createState() => _DataFromAPIState();
}
List<Attributes> MyAllData = [];
class _DataFromAPIState extends State<DataFromAPI> {
@override
void initState() {
super.initState();
}
Future<List<Sr>> loadData() async {
try {
var response = await http.get(Uri.parse(
'http://192.168.1.30:9080/maxrest/rest/mbo/sr/?_lid=azizl&_lpwd=max12345m&_format=json'));
if (response.statusCode == 200) {
final jsonBody = json.decode(response.body);
Demandes data = Demandes.fromJson(jsonBody);
final srAttributes = data.srMboSet.sr;
return srAttributes;
}
} catch (e) {
throw Exception(e.toString());
}
throw Exception("");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: new Scaffold(
appBar: AppBar(
title: Text('Liste des Demandes'),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.push(
context, MaterialPageRoute(builder: (context) => SR()))),
),
body: FutureBuilder<List<Sr>?>(
future: loadData(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
} else {
return new ListView.builder(
itemCount: snapshot.data?.length,
itemBuilder: ((_, index) {
return new ListTile(
title: new Card(
child: new ListTile(
title: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(
'Ticket ID : ${snapshot.data![index].attributes.ticketid.content}'),
new Text(
'status : ${snapshot.data![index].attributes.status.content}'),
new Text(
'reportedby : ${snapshot.data![index].attributes.reportedby.content}'),
new Text(
'Status Date : ${snapshot.data![index].attributes.statusdate.content}'),
new Text(
'Status Date : ${snapshot.data![index].attributes.description.content}'),
],
),
),
),
onTap: () {
Navigator.of(context).push(
new MaterialPageRoute(
builder: (BuildContext context) =>
new SrDetailsScreen(
sr: snapshot.data![index]),
),
);
});
}));
}
},
),
),
);
}
}
CodePudding user response:
Affecteddate? description;
//instead of
Affecteddate description;
here you're telling the complier it's ok to have null value i know what I'm doing.
CodePudding user response:
The difference between Affecteddate
and Affecteddate?
is determined by null safety.
When you opt into null safety, types in your code are non-nullable by default, meaning that variables can’t contain null unless you say they can.
With it, variables cannot be null if they haven't got a ?
at the end. For example:
String myString = "Hello World"; //This CANNOT be null
String? myString = null; //This CAN be null
In your code, you will need to check if your Affecteddate?
is null before using it.
You can use a null check !
as:
Affecteddate? myOldVar = null;
Affecteddate myNewVar = myOldVar!; //If myOldVar is null, an Exception will be thrown.
or a ternary operator:
Affecteddate? myOldVar = null;
Affecteddate myNewVar = myOldVar ?? Affecteddate(content: ""); //If myOldVar is null, create a new (empty) Affecteddate.