Home > OS >  Instance of 'Future<GroupBy>' in my flutter code
Instance of 'Future<GroupBy>' in my flutter code

Time:10-31

from the original code I can't get the same result

here is the original code

 loadPreviousEvents() {
    mySelectedEvents = {
      "2022-09-13": [
        {"eventDescp": "11", "eventTitle": "111"},
        {"eventDescp": "22", "eventTitle": "22"}
      ],
      "2022-09-30": [
        {"eventDescp": "22", "eventTitle": "22"}
      ],
      "2022-09-20": [
        {"eventTitle": "ss", "eventDescp": "ss"}
      ]
    };
    print(mySelectedEvents);
  }

I want to retrieve the same thing from my database

here is what i tried without success

loadPreviousEvents() async {
    var url = 'http://prospection.global-aeit.com/getEvents.php';
    var res = await http.get(Uri.parse(url));
    var response = json.decode(res.body) as List;
    print(response);
    var mySelectedEvents =
        (response.map((e) async => GroupBy.fromJson(e))).toList();
    print(mySelectedEvents);
    return mySelectedEvents;

  }

by making print of print(mySelectedEvents);

[Instance of 'Future<GroupBy>', Instance of 'Future<GroupBy>', Instance of 'Future<GroupBy>', Instance of 'Future<GroupBy>', Instance of 'Future<GroupBy>']

print of print(response); gives

[{date: 2022-09-17, eventDescp: azerty, eventTitle: azertyui}, {date: 2022-09-17, eventDescp: 11, eventTitle: AZE}, {date: 2022-09-17, eventDescp: 22, eventTitle: 4556}, {date: 2022-09-20, eventDescp: 77, eventTitle: HHJ}, {date: 2022-09-17, eventDescp: 44, eventTitle: BYYY}]

CodePudding user response:

You're forcing the map method to return a Future because of the async keyword.

Remove it and you'll be fine.

Test the following it helps you:

Future<void> main() async {
  final res = await dbRequest();
  print(res);
  final mySelectedEvents = (res.map((e) => GroupBy.fromJson(e))).toList();
  print(mySelectedEvents);
}

Future<List<dynamic>> dbRequest() async {
  return [
    {'date': '2022-09-17', 'eventDescp': 'azerty', 'eventTitle': 'azertyui'},
    {'date': '2022-09-17', 'eventDescp': '11', 'eventTitle': 'AZE'},
    {'date': '2022-09-17', 'eventDescp': '22', 'eventTitle': '4556'},
    {'date': '2022-09-20', 'eventDescp': '77', 'eventTitle': 'HHJ'},
    {'date': '2022-09-17', 'eventDescp': '44', 'eventTitle': 'BYYY'}
  ];
}

CodePudding user response:

Unresolved issue

[Instance of 'GroupBy', Instance of 'GroupBy', Instance of 'GroupBy', Instance of 'GroupBy', Instance of 'GroupBy']

    @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _selectedDate = _focusedDay;

    loadPreviousEvents();
  }

  Future<void> loadPreviousEvents() async {
    print("object");
    final res = await dbRequest();
    final mySelectedEvents = (res.map((e) => GroupBy.fromJson(e))).toList();
    print(mySelectedEvents);
  }

  Future<List<dynamic>> dbRequest() async {
    var url = 'http://prospection.global-aeit.com/getEvents.php';
    var res = await http.get(Uri.parse(url));
    var response = json.decode(res.body);
    print(response);
    return response;}
  • Related