Skip to main content

Get Categories from API into ListView: http, Future, await/async

Premium
16:44

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

BM
Ben McKay ✓ Link copied!

I have been stuck on this lesson for a few hours trying to troubleshoot. I continue to get a "Something went wrong" message. I made the update to Uri so it's not that. I have restarted Android Studio, Emulator, etc.

What ended up being the issue is that I had to change from using Laravel Valet and use 'php artisan serve', then instead of localhost:8000, I had to use 10.0.2.2:8000. See link explaining in better detail: https://medium.com/@podcoder/connecting-flutter-application-to-localhost-a1022df63130

PK
Povilas Korop ✓ Link copied!

Thanks Ben for commenting it so others will learn. Not sure why it worked for me during shooting of this course, I don't remember this problem.

BM
Ben McKay ✓ Link copied!

of course i think it looks like you are using an actual server with domain at this point in the tutorial so that may be why

S
Slon ✓ Link copied!

I had a similar problem when using laravel valet. The problem was solved when I disabled https (valet unsecure).

I
indebit ✓ Link copied!

I was keep getting "Something went wrong", then I printed the error from from snapshot and I have this error:

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>
G
gmrk08 ✓ Link copied!

have you solve this error? I got it too.

G
gmrk08 ✓ Link copied!

I got it, the error is in the api response...

flutter must handle the api response which is : Example:

{" > data":[ > { > "id":5,"name":"Categori2" > } > ] }

I
indebit ✓ Link copied!

I gave up at some point and tried to download the full final code, but there were some other errors and I decided to learn more Flutter. I'll give it a try with your fix. Thank you

UP
Unisoft Prima ✓ Link copied!

you can unwrap 'data' in your laravel api. Add this code in your AppServiceProvider.php

public function boot() { // Remove JSON bracket "data" JsonResource::withoutWrapping(); }

Or simply add this code in flutter: final data = jsonDecode(response.body); List categories = data['data'];

G
gmrk08 ✓ Link copied!

I recommend for following this video..

use this site [https://jsonplaceholder.typicode.com/] for api testing:

https://my-json-server.typicode.com/typicode/demo/posts

M
mnairat ✓ Link copied!

Hello all,

I faced the same problem, and after some search it turned out that in order for Android Studio to be able to connect to the localhost, it must use IP 10.0.2.2 and restart the emulator. The link I used and correctly worked like this http://10.0.2.2:8000/api/categories. 10.0.2.2 it's just like 127.0.0.1 in emulator.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.