Himanshu Ranjan
Himanshu Ranjan

Reputation: 302

How to solve insecure http not allowed by platform flutter?

I am trying to fetch some data from my custom made rest api and i am having a lot of trouble with this error. I even tried the google official migration guide.

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

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String PROTOCOL = "http";
  String DOMAIN = "192.168.0.5:5000";

  Future<List<dynamic>> fetchUsers() async {
    var url = "$PROTOCOL://$DOMAIN/posts";
    var result = await http.get(url);
    print(result);
    return json.decode(result.body);
  }

  @override
  Widget build(BuildContext context) {
    fetchUsers();
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        child: FutureBuilder<List<dynamic>>(
          future: fetchUsers(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return Text(snapshot.data[index]['description']);
                },
                itemCount: snapshot.data.length,
              );
            } else
              return Container();
          },
        ),
      ),
    );

My server is up and running and working good with postman but facing troubles with flutter.Need some serious help here!

Upvotes: 3

Views: 4184

Answers (3)

Sarthak Singhal
Sarthak Singhal

Reputation: 1225

Add these lines to activity tag of AndroidManifest.xml

 android:usesCleartextTraffic="true"
 android:networkSecurityConfig="@xml/network_security_resource"

In res, create a new folder named xml in res folder

Create a new xml file named network_security_resource.xml in xml folder

Add the below code to it-

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
    <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
</base-config>
</network-security-config>

Then follow the steps given here https://flutter.dev/docs/release/breaking-changes/network-policy-ios-android https://developer.android.com/training/articles/security-config

If the above steps does not work. Try changing flutter branch and upgrade it.

I did both the things and now its working. I am on Flutter 1.22.0 • channel stable

Upvotes: 4

Vaibhav Pallod
Vaibhav Pallod

Reputation: 524

Simple Solution to this

I was using

final String baseUrl='http://......herokuapp.com/';

this url that was giving me this exception but then i did http to https

final String baseUrl='https://......herokuapp.com/';

that's due to security purpose of some mobiles

and that worked for me thanks in advance !

Upvotes: 1

Dolphin
Dolphin

Reputation: 38621

To temp fix your problem. First add a config in res/xml(my full path is /Users/dolphin/source/third-party/Cruise/android/app/src/main/res/xml) folder:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

then in your AndroidManifest.xml file add this config:

android:networkSecurityConfig="@xml/network_security_config"

this could allow http traffic. But it just using to debug, the best way is to using https traffic. more info: How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?

It have solve my same problem. Hope this help for you!

Upvotes: 1

Related Questions