Home > Software engineering >  not able to access json file in flutter api call
not able to access json file in flutter api call

Time:10-10

I want to use JSON from this link https://howtodoandroid.com/movielist.json.

This is my code where I want to call API from the above link

Future getMovieList() async {

final response = await http.get(Uri.parse("https://howtodoandroid.com/movielist.json"));
if (response.statusCode == 200) {
  allMovielist = jsonDecode(response.body);
 
  
    }
}

i got error "failed to load response data: no data found for resource with given identifier"

CodePudding user response:

Future<List<YourModel>> getMovieList() async {
final response = await http.get(Uri.parse("https://howtodoandroid.com/movielist.json"));
if (response.statusCode == 200) {
  /// On first the data will get is always unassigned or just dynamic
  /// which cant be identify 
  final List<dynamic> list = jsonDecode(response.body);
   /// try to print or log the data if the data goes to status 200 and able
   /// to see it
   log(list.map((e) => e).toList().toString());
   /// this one will return the indicate assigned data
   return list.map((e)=> YourModel.fromJson(e)).toList();
  } else{
  return [];
 }
}

CodePudding user response:

If you are trying this on the web run this command flutter run -d chrome --web-renderer html

if you still get a CROS error disable the web security.

1- Go to flutter\bin\cache and remove a file named: flutter_tools.stamp

2- Go to flutter\packages\flutter_tools\lib\src\web and open the file chrome.dart.

3- Find '--disable-extensions'

4- Add '--disable-web-security'

I get this answer from this link

or consider this package also

Example Code

 Future getMoviesList() async {
    var headers = <String, String>{'Content-Type': 'application/json'};
    var url = Uri.parse('https://howtodoandroid.com/movielist.json');
    var response = await http.get(url, headers: headers);
    if (response.statusCode == 200) {
      var result = json.decode(response.body);
      print(result);
      return result;
    }
  }
  • Related