Courses

Flutter v2 Mobile App with Laravel API

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

Previous: Back to the Default main.dart: Now You Understand?
avatar

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

👍 2
avatar

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.

avatar

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

avatar

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

avatar
You can use Markdown
avatar

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>
avatar

have you solve this error? I got it too.

avatar

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

flutter must handle the api response which is : Example:

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

avatar

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

avatar

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'];

avatar
You can use Markdown
avatar

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

avatar
You can use Markdown
avatar

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.

avatar
You can use Markdown
avatar
You can use Markdown