Home > Mobile >  how to update (instead of add) the data in the container when requesting GET? in flutter
how to update (instead of add) the data in the container when requesting GET? in flutter

Time:10-12

I have a functional. In which I make a request to Get. Then I parse the answer from this query (json), and this data is displayed in my widget. It all works. But if I press the button again. Data will not be updated. And the updated data that I will receive will be added to those that are already in the widget list. I would like that on the button the data are updated (that is that the data were written down only those which I took away with request GET last time, and the old data were deleted). My request:

List<eventHistory> _userDetails = [];
 
  Future<Null> getEventsTTNDetails(int? ttn) async {
    HttpClient client = new HttpClient();
    client.badCertificateCallback =
    ((X509Certificate cert, String host, int port) => true);
    final String urlEvents = global.urlVar   "/ttn_events";

    final request = await client
        .getUrl(Uri.parse(urlEvents))
        .timeout(Duration(seconds: 5));

    HttpClientResponse response = await request.close();


    var responseBody = await response.transform(utf8.decoder).join();


    final responseJson = json.decode(responseBody);

    setState(() {
      for (Map<String, dynamic> user in responseJson) {
        _userDetails.add(eventHistory.fromJson(user));
      }
    });
  }

my class where I parse the answer:

class eventHistory {

  final String date, event,device;



  eventHistory({required this.date, required this.event, required this.device});

  factory eventHistory.fromJson(Map<String, dynamic> json) {
    return new eventHistory(
      date: json['date'],
      event: json['event'],
      device: json['device'],
    );
  }
}

The widget in which I display this data:

                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Container(
                                height: 45,
                                child: Text("history", style: TextStyle(
                                    fontSize: 25.0, color: Colors.blueGrey)),
                                padding: EdgeInsets.all(2.0)),
                            Expanded(
                                child: Scrollbar(
                                    child: new ListView(
                                      shrinkWrap: true,
                                      children: [
                                        new Container(
                                            alignment: Alignment.bottomLeft,
                                            child: FixedTimeline.tileBuilder(
                                              theme: TimelineThemeData(
                                                nodePosition: 0.23,
                                                color: Colors.blueGrey,
                                                indicatorTheme: IndicatorThemeData(
                                                  position: 2,
                                                  size: 20.0,
                                                ),
                                                connectorTheme: ConnectorThemeData(
                                                  thickness: 2.5,
                                                ),
                                              ),
                                              builder: TimelineTileBuilder
                                                  .connectedFromStyle(
                                                contentsAlign: ContentsAlign
                                                    .basic,
                                                oppositeContentsBuilder: (
                                                    context, index) =>
                                                    Padding(
                                                      padding: const EdgeInsets
                                                          .all(8.0),
                                                      child: Text(
                                                          _userDetails[index]
                                                              .date),
                                                    ),
                                                contentsBuilder: (context,
                                                    index) =>
                                                    Card(
                                                      child: Padding(
                                                        padding: const EdgeInsets
                                                            .all(8.0),
                                                        child: Text(
                                                            _userDetails[index]
                                                                .event),
                                                      ),
                                                    ),
                                                connectorStyleBuilder: (context,
                                                    index) =>
                                                ConnectorStyle.solidLine,
                                                indicatorStyleBuilder: (context,
                                                    index) =>
                                                IndicatorStyle.dot,
                                                itemCount: _userDetails.length,
                                              ),
                                            )
                                        ),

                                      ],
                                    ))),
                          ],
                        )

CodePudding user response:

You are directly adding the new data to _userDetails You can use another List to change JSON to eventHistory and then set the list to _userDetails

List<eventHistory> _userDetails = [];
 
  Future<Null> getEventsTTNDetails(int? ttn) async {
    HttpClient client = new HttpClient();
    client.badCertificateCallback =
    ((X509Certificate cert, String host, int port) => true);
    final String urlEvents = global.urlVar   "/ttn_events";

final request = await client
    .getUrl(Uri.parse(urlEvents))
    .timeout(Duration(seconds: 5));

HttpClientResponse response = await request.close();


var responseBody = await response.transform(utf8.decoder).join();


final responseJson = json.decode(responseBody);
List<eventHistory> _temp=[];
  for (Map<String, dynamic> user in responseJson) {
    _temp.add(eventHistory.fromJson(user));
  }
     setState(() {
   _userDetails=_temp;
});
}
  • Related