Flutter: After build APK, data from API doesn't appear

2,054

The issue you are facing because when you are in the debugging mode, the debug mode has the Android manifest file which has the internet permission by default, but there is the main folder in the src in which there is another manifest file where you have not given the Internet permission. As MePo said that you should give the internet permission

<uses-permission android:name="android.permission.INTERNET"/>
Share:
2,054
Maguro97
Author by

Maguro97

Updated on December 18, 2022

Comments

  • Maguro97
    Maguro97 over 1 year

    I’m trying to create an App that shows some data coming from an API. The problem is that, while App works well in debugging mode using emulator or smartphone, without showing any errors. If I build APK the App doesn't download data or at least it doesn't show them. How can I solve this issue?

    Some more details:

    • I built APK more times
    • I installed APK in three different smartphone, also of different brands
    import 'dart:convert';
    
    import 'package:apiitest2/models/Obj.dart';
    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    
    
    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
      List<Obj> dataDef = List<Obj>();
    
      void getData() {
        List<Obj> dataDef0 = List<Obj>();
        http.get(Uri.encodeFull("https://jsonplaceholder.typicode.com/posts"),
            headers: {"Accept": "application/json"}).then((resp) {
          List data = json.decode(resp.body);
          for (var item in data) {
            Obj obj = Obj(item["userId"], item["id"], item["title"], item["body"]);
            dataDef0.add(obj);
          }
          setState(() {
            dataDef = dataDef0;
          });
        });//.catchError((onError){});
      }
    
      @override
      void initState() {
        super.initState();
        getData();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: CustomScrollView(slivers: <Widget>[
              SliverAppBar(
                backgroundColor: Colors.red,
                floating: false,
                pinned: true,
                expandedHeight: 200.0,
                flexibleSpace: FlexibleSpaceBar(
                    centerTitle: true,
                    title: Text(
                      "Title",
                      style: TextStyle(
                        color: Colors.black87,
                        fontSize: 20.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    background: Container(
                      color: Colors.amber,
                    )
                ),
              ),
              body()
            ]
            )
        );
      }
    
      Widget body() {
        if (dataDef.isEmpty)
          return SliverToBoxAdapter(
              child: SizedBox(
                height: MediaQuery.of(context).size.height - 200,
                child: Center(
                    child: Container(
                        height: 70,
                        width: 70,
                        child: CircularProgressIndicator()
                    )
                ),
              )
          );
        else {
          return SliverList(delegate:
          SliverChildBuilderDelegate((BuildContext context, int index) {
            if (index > dataDef.length - 1) return null;
            return Container(
              child: Text(dataDef[index].title),
              height: 50,
            );
          }));
        }
      }
    }
    
    • MePo
      MePo about 4 years
      Did you check in manifest.xml if you have all permissions? <uses-permission android:name="android.permission.INTERNET"/>
  • Sagar Acharya
    Sagar Acharya about 4 years
    Thats nice it worked for you just make an upvote so that it will be helpful for others
  • Visakh Vijayan
    Visakh Vijayan over 3 years
    You are the closest thing to God that I know at this moment. Thanks, man have been stuck with the graphql bug for 4 hours now. Solved by this one liner. Thank you so myuch