0

I develop a flutter application in my laptop, and try to get data from rest api, Ok, for showing data in chrome I add '--disable-web-security' this linkin spesific file and it work fine to show data from api to chrome emulator, but how can i show data in android emulator, because android emulator dont show data,

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
  title: 'Flutter Demo',
  debugShowCheckedModeBanner: false,
  theme: ThemeData(
    primarySwatch: Colors.blue,
  ),
  home: const MyHomePage(),
  );
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
 State<MyHomePage> createState() => _MyHomePageState();
 }

class _MyHomePageState extends State<MyHomePage> {
var items = [];

@override
void initState() {
 refreshUser();
 super.initState();
}

 Future refreshUser() async {
//Uri uri = Uri.parse('https://localhost:44305/api/CarModel');


 final response = await http.get(uri, headers: {
  'Content-type': 'application/json',
  "Access-Control-Allow-Origin": "*", // Required for CORS support to work
  "Access-Control-Allow-Credentials":
      'true', // Required for cookies, authorization headers with HTTPS
  "Access-Control-Allow-Headers":
      "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
  "Access-Control-Allow-Methods": "POST, GET,OPTIONS"
  });
   var data = json.decode(response.body);

  items = [];
  setState(() {
   for (var i = 0; i < data.length; i++) {
     items.add(data[i]['name']);
   }
  });
  }

  @override
  Widget build(BuildContext context) {
  return Scaffold(
   body: RefreshIndicator(
    onRefresh: refreshUser,
    child: ListView.builder(
      itemCount: items.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text(items[index]),
        );
      },
    ),
  ),
);
}
}
sunny
  • 2,670
  • 5
  • 24
  • 39
  • 1
    `--disable-web-security` basically tells your web browser to ignore CORS restrictions, you don't need to do the same thing on android/ios because those platforms do not have CORS restrictions. One thing you should check on android though, is that you have `` in your `AndroidManifest.xml`. – mmcdon20 Dec 29 '21 at 22:00
  • Remember to do the equivalent of this in iOS if you're planning to release there. – Timotej Leginus Dec 30 '21 at 00:57

0 Answers0