0

I am coding a website and an app for the same purpose. I am fluent at PHP but new to dart. Both, the website and the app call an API to get the data they should display. In PHP I am using a foreach loop to go through and echo a div for each key just like this:

# res is an example string for an api result
$res = '{"first":["ExampleTitle", "ExampleDescription"], "second":["ExampleT2", "ExampleD2"], "third":["ExpampleT3", "ExampleD3"]}';
$res = json_decode($res);

foreach ($res as $key => $value) {
  echo '<div style="margin-bottom: 35px;">'.$value[0].'<br>'.$value[1].'</div>';
}

and the output looks like this:

ExampleTitle
ExampleDescription

ExampleT2
ExampleD2

ExpampleT3
ExampleD3

Now I want to do the same thing in the app using dart. How can I loop trough an object to create multiple containers?

In other words: what dart code would achieve the same thing the PHP code does, except instead of creating divs it creates containers?

  • 1
    you cant handle html-like data in flutter. your api should return your data as json, and you should convert it in flutter and build the ui accordingly. [you can checkout this page for more info](https://flutter.dev/docs/cookbook/networking/fetch-data) – Adnan May 25 '21 at 17:41
  • This might help answer your question: – Kamva Moletsane May 25 '21 at 18:33
  • 1
    @Adnan the data is in the correct json format, thats not the problem but the linked page was still helpful. Thank you! – Ella Ridman May 25 '21 at 20:22

0 Answers0