0

That title error is pointed on this line:

Text('${intData['summary']['intrested']}')

I want to display here data from backend, but I don't know how to accomplish that I am getting this error, but this is not null.

Here is my code:


var intData;

Future<Map> loadMyFriends(String eventId) async {
    
    var request = await EndPoints.interest(eventId, userId);
    print(request.body);
    intData = jsonDecode(request.body)['data'] as Map;
    return jsonDecode(request.body)['data'];
  }


@override
  void initState() {
    super.initState();
    userId = widget.userId;
    loadMyFriends(widget.partyMap['id']).then((value) => print('$intData'));
  }

@override
  void didUpdateWidget(SquareBoxMovableBuilder oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.partyMap != oldWidget.partyMap) {
      loadMyFriends(widget.partyMap['id']);
      print(intData);
    }
  }

and here are console prints what intData contains:

//print(request.body) in loadMyFriends function
{"data":{"intrested":null,"participate":null,"summary":{"intrested":0,"participate":0},"friends":{"list":[],"sum":0}}}

//print(intData) in initState
{intrested: null, participate: null, summary: {intrested: 0, participate: 0}, friends: {list: [], sum: 0}}

//print(intData) in didUpdateWidget
{intrested: null, participate: null, summary: {intrested: 0, participate: 0}, friends: {list: [], sum: 0}}

I tried to do this with FutureBuilder but I had same result. How to fix it or how to do it? How to display data downloaded from backend every time when I open this widget?

1 Answers1

0

While the loadMyFriends is a future method and it will take some time to get data and after getting data you need to call setState((){}) to update the UI. You can try on

Future<Map> loadMyFriends(String eventId) async {
    
    var request = await EndPoints.interest(eventId, userId);
    print(request.body);
    intData = jsonDecode(request.body)['data'] as Map;
    setState((){}); //here
    return jsonDecode(request.body)['data'];
  }
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56