简体   繁体   中英

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

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,
        );
      }));
    }
  }
}

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM