0

I want to display the 'name' of groupes (groupID) into listView, the names of device that i want to display are (demo,4v,demo2,doker...) from DeviceGroup table, using XML webservice.i used the xmljson to convert it, plz Can someone correct my faults.

My Xml web service:

<GTSResponse command="dbget" result="success">
    <Record table="DeviceGroup">
        <Field name="accountID" primaryKey="true">
            <![CDATA[demo]]>
        </Field>
        <Field name="groupID" primaryKey="true">
            <![CDATA[demo]]>
        </Field>
        <Field name="PushpinsLimitGroup">0</Field>
        <Field name="ReportsLimitGroup">0</Field>
        <Field name="displayName"></Field>
        <Field name="description">
            <![CDATA[4 v]]>
        </Field>
        <Field name="notes"></Field>
        <Field name="lastUpdateTime">1529258380</Field>
        <Field name="creationTime">1498727371</Field>
        <Field name="workOrderID"></Field>
        <Field name="allowNotify">false</Field>
        <Field name="notifyEmail"></Field>
        <Field name="parentGroupID"></Field>
    </Record>
    <Record table="DeviceGroup">
        <Field name="accountID" primaryKey="true">
            <![CDATA[demo]]>
        </Field>
        <Field name="groupID" primaryKey="true">
            <![CDATA[demo2]]>
        </Field>
        <Field name="PushpinsLimitGroup">0</Field>
        <Field name="ReportsLimitGroup">0</Field>
        <Field name="displayName"></Field>
        <Field name="description"></Field>
        <Field name="notes"></Field>
        <Field name="lastUpdateTime">1555499492</Field>
        <Field name="creationTime">1555499492</Field>
        <Field name="workOrderID"></Field>
        <Field name="allowNotify">false</Field>
        <Field name="notifyEmail"></Field>
        <Field name="parentGroupID"></Field>
    </Record>
    <Record table="DeviceGroup">
        <Field name="accountID" primaryKey="true">
            <![CDATA[demo]]>
        </Field>
        <Field name="groupID" primaryKey="true">
            <![CDATA[doker]]>
        </Field>
        <Field name="PushpinsLimitGroup">0</Field>
        <Field name="ReportsLimitGroup">0</Field>
        <Field name="displayName"></Field>
        <Field name="description"></Field>
        <Field name="notes"></Field>
        <Field name="lastUpdateTime">1528138728</Field>
        <Field name="creationTime">1528138728</Field>
        <Field name="workOrderID"></Field>
        <Field name="allowNotify">false</Field>
        <Field name="notifyEmail"></Field>
        <Field name="parentGroupID"></Field>
    </Record>
    <Record table="DeviceGroup">
        <Field name="accountID" primaryKey="true">
            <![CDATA[demo]]>
        </Field>
        <Field name="groupID" primaryKey="true">
            <![CDATA[glowex]]>
        </Field>
        <Field name="PushpinsLimitGroup">0</Field>
        <Field name="ReportsLimitGroup">0</Field>
        <Field name="displayName"></Field>
        <Field name="description"></Field>
        <Field name="notes"></Field>
        <Field name="lastUpdateTime">1604598739</Field>
        <Field name="creationTime">1604598739</Field>
        <Field name="workOrderID"></Field>
        <Field name="allowNotify">false</Field>
        <Field name="notifyEmail"></Field>
        <Field name="parentGroupID"></Field>
    </Record>
    <Record table="DeviceGroup">
        <Field name="accountID" primaryKey="true">
            <![CDATA[demo]]>
        </Field>
        <Field name="groupID" primaryKey="true">
            <![CDATA[inter]]>
        </Field>
        <Field name="PushpinsLimitGroup">0</Field>
        <Field name="ReportsLimitGroup">0</Field>
        <Field name="displayName"></Field>
        <Field name="description"></Field>
        <Field name="notes"></Field>
        <Field name="lastUpdateTime">1579869475</Field>
        <Field name="creationTime">1579869475</Field>
        <Field name="workOrderID"></Field>
        <Field name="allowNotify">false</Field>
        <Field name="notifyEmail"></Field>
        <Field name="parentGroupID"></Field>
    </Record>
    <Record table="DeviceGroup">
        <Field name="accountID" primaryKey="true">
            <![CDATA[demo]]>
        </Field>
        <Field name="groupID" primaryKey="true">
            <![CDATA[supporttest]]>
        </Field>
        <Field name="PushpinsLimitGroup">0</Field>
        <Field name="ReportsLimitGroup">0</Field>
        <Field name="displayName"></Field>
        <Field name="description"></Field>
        <Field name="notes"></Field>
        <Field name="lastUpdateTime">1599144965</Field>
        <Field name="creationTime">1599144965</Field>
        <Field name="workOrderID"></Field>
        <Field name="allowNotify">false</Field>
        <Field name="notifyEmail"></Field>
        <Field name="parentGroupID"></Field>
    </Record>
</GTSResponse>

Function to get data from web service

getDataGroupe() async {
    String message = "<GTSRequest command='dbget'><Authorization account='demo' user='xxxxxx' password='xxxxxxx'/><Record table='DeviceGroup' partial='true'><Field name='accountID'>demo</Field></Record></GTSRequest>";
    print("message: $message");

    String url = "http://xxxxxxxxxxxx";

    final http.Response response =
    await http.post(Uri.parse(url), body: message, headers: {
      'Content-type': 'text/xml',
    });

    Provider.of<APIProvider>(context, listen: false).setAccount(account);
    Provider.of<APIProvider>(context, listen: false).setUser(user);
    Provider.of<APIProvider>(context, listen: false).setPassword(password);

    if (response.statusCode == 200) {
      print(response.body);

      final Xml2Json xml2Json = Xml2Json();
      xml2Json.parse(response.body);
      var jsonString = xml2Json.toParker();
      print(jsonString);

      var  data= jsonDecode(jsonString);
       list = data['GTSResponse']['Record']['Field'];
      for(var item in list){
        print(item);
      }
    }
  }

listview

@override
  Widget build(BuildContext context) {
  
    return Scaffold(

      body:  new Expanded(
        child: ListView.builder(
          itemCount: list == null ? 0 : list.length,
          itemBuilder: (BuildContext context, int index) {
            return new ListTile(
              title: new Card(
                elevation: 5.0,
                child: new Container(
                  alignment: Alignment.centerLeft,
                  margin: new EdgeInsets.only(
                      top: 10.0, bottom: 10.0, left: 15),
                  child: new Text(
                     null  //data['GTSResponse']['Record'],
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

the result in the console

i think this path of deviceID name that i used list = data['GTSResponse']['Record']['Field']; not correct enter image description here

the result after convert xml data to json, me i want to display the second element after "Field" (demo,demo2, doker,glowex,inter, and suportest)

{"GTSResponse": {"Record": [{"Field": ["demo", "demo", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]}, {"Field": ["demo", "demo2", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]}, {"Field": ["demo", "doker", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]}, {"Field": ["demo", "glowex", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]}, {"Field": ["demo", "inter", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]}, {"Field": ["demo", "supporttest", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]}]}}
2
  • Please specify the error you are getting. Commented Jun 3, 2021 at 11:40
  • i add it,i think this path of deviceID name that i used list = data['GTSResponse']['Record']['Field']; not correct Commented Jun 3, 2021 at 11:58

1 Answer 1

1

The Error is that data['GTSResponse']['Record'] is a list itself and you are trying to provide a string as the index to access.

It should be like

data['GTSResponse']['Record'][0]['Field'][1];

to access the 2nd field (index 1) of first record,

data['GTSResponse']['Record'][1]['Field'][1];

for the second record and so on.


Try this:

var recordList = List.of(data['GTSResponse']['Record']); // Get the records
var requiredList = recordList.map<String>((record) => record['Field'][1]).toList(); // Generate the list of 2nd elements (index 1) of field of every record.

print(requiredList);

// Output: [demo, demo2, doker, glowex, inter, supporttest]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.