This is my code :
String text = snapshot.data[index];
var splitText = text.split("\n") ;
final jdata = jsonEncode(splitText[5]);
print(jdata);
which prints the String :
"RunHyperlink : {classid: 25510, id: 2, mad_key: 32835}"
snapshot.data[index] contains :
I/flutter (14351): Code : LE-0000000002
I/flutter (14351): Description : test_01
I/flutter (14351): Organisation Unit : 01_01_04_01_SA - Shah Alam
I/flutter (14351): Date Reported : 18/09/2020
I/flutter (14351): Status : LE110 - Pending Approval
I/flutter (14351): RunHyperlink : {classid: 25510, id: 2, mad_key: 32835}
my question is how do I access the value of "id".
Thanks!
CodePudding user response:
I'm assuming that string is not changing in any condition other wise logic may fail..
void main() {
// actual string
// String x="RunHyperlink : {classid: 25510, id: 2, mad_key: 32835}";
String x = snapshot.data[index];
// remove unwanted substring form string
List keyValues= x.replaceAll('RunHyperlink : {','').replaceAll('}','').replaceAll(', ',',').split(",");
//create map
Map map = {};
// run for loop to split key and value
for (var element in keyValues){
print(element.split(": ")[0]);
map.addAll({element.split(": ")[0] : element.split(": ")[1]});
}
// get the id balue
print(map['id']);
}
CodePudding user response:
String temp;
const start = " id:";
const end = ",";
if (str.contains(start)) {
final startIndex = str.indexOf(start);
final endIndex = str.indexOf(end, startIndex start.length);
temp = str.substring(startIndex start.length, endIndex);
print("ID $temp");
}
or
Iterable<Match>? matches = RegExp(r' id\W*([a-z0-9\s] ,)').allMatches(str);
for (var m in matches) {
print(m.group(0));
}