Following is the code.On the OnPressed() method of it new document is getting created and its DocId is also getting created which gives following response:
[{"docId":630,"tokenNo":26000111111111,"docTitle":"Samrudhhi ","address":"kharadi","city":"pune","partyName":"Priyanshi Chavan","pinCode":"428000","docType":"Commercial","duration":"36","startDate":"2022-12-03","endDate":"2025-12-02","docStatus":"Open","rentDesc":"2300","createdBy":null,"updatedBy":null}],
I wants to get the docId of it as soon as the new document creates how to get it ? I wants to use that docId to navigate to document detail page when user clicks on the Push Notification of one signal. User will get notified once he add new document and when he clicks on that particular notification he should get navigated to the details page of particular document.
Container(
//alignment: Alignment.center,
height: 35,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
color: Color.fromARGB(255, 3, 89, 168),
),
child: ElevatedButton(
onPressed: () async {
formKey.currentState?.validate();
final isValidForm = formKey.currentState!.validate();
if (isValidForm) {
//formKey.currentState?.validate();
// final int docId = ;
final String docTitle = docTitleController.text;
final int tokenNo =
int.parse(tokenNoController.text).toInt();
//final String tokenNo = tokenNoController.text;
final String partyName = partyNameController.text;
// addStatus(selectedItem!);
final String docType = selectedValue.toString();
final String startDate = startDateController.text;
final String endDate = endDateController.text;
Map data = {
'docTitle': docTitle,
'tokenNo': tokenNo,
'partyName': partyName,
'startDate': startDate,
'endDate': endDate,
};
final body = json.encode(data);
var response = http.post(
Uri.parse(
'http://3.108.194.111:8080/AtdochubJ-3/document/register'),
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: body);
List<Document> res =
await DocumentController.fetchDocList();
this.DID = res[0].docId;
print('new Document id ${this.DID}');
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => DocumentPage()));
initOneSignal();
child: const Text("Save"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
Color.fromARGB(255, 3, 89, 168))),
))
],
),
);
}
Future<void> initOneSignal() async {
OneSignal.shared.setAppId('e89acaa4-5388-4e3a-bd69-44d197bdcbd7');
final status = await OneSignal.shared.getDeviceState();
final String? osUserID = status?.userId;
//var status = await OneSignal.shared.getPermissionSubscriptionState();
// var playerId = status.subscriptionStatus.userId;
OneSignal.shared
.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
debugPrint('NOTIFICATION OPENED HANDLER CALLED WITH: $result');
setState(() {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => DocumentsDetails(
this.DID,
)));
});
});
await OneSignal.shared.postNotification(OSCreateNotification(
playerIds: [osUserID!],
content: "New Document Added",
heading: "'${this.docTitleController.text}'",
buttons: [
OSActionButton(text: "View", id: "DocView"),
]));
}
FutureBuilder<Document> buildFutureBuilder() {
return FutureBuilder<Document>(
future: _futureDocument,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!.docTitle);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
);
}
}