简体   繁体   English

Flutter:构建 APK 后,API 中的数据不会出现

[英]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.我正在尝试创建一个应用程序来显示来自 API 的一些数据。 The problem is that, while App works well in debugging mode using emulator or smartphone, without showing any errors.问题是,虽然 App 在使用模拟器或智能手机的调试模式下运行良好,但没有显示任何错误。 If I build APK the App doesn't download data or at least it doesn't show them.如果我构建 APK,该应用程序不会下载数据,或者至少不会显示它们。 How can I solve this issue?我该如何解决这个问题?

Some more details:更多细节:

  • I built APK more times我多次构建 APK
  • I installed APK in three different smartphone, also of different brands我在三款不同品牌的智能手机上安装了 APK
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.您面临的问题是因为当您处于调试模式时,调试模式具有默认具有 Internet 权限的 Android 清单文件,但src中有主文件夹,其中有另一个清单文件,您没有获得互联网许可。 As MePo said that you should give the internet permission正如 MePo 所说,您应该授予互联网许可

<uses-permission android:name="android.permission.INTERNET"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 应用程序在使用 flutter build apk --split-per-abi --no-sound-null-safety 构建后未从 REST API 获取任何数据 - Flutter app not fetching any data from REST API after building it with flutter build apk --split-per-abi --no-sound-null-safety 将 flutter 从 master 切换到 stable 后无法构建发布 apk - Can not build release apk after switchinf flutter from master to stable Flutter Firestore 未检索已发布的 android apk 中的任何数据 - Flutter Firestore doesn't retrieve any data in released android apk 无法在 Flutter 中构建 APK 文件 - Can't build APK file in Flutter Flutter build apk 不起作用并显示 Gradle 任务 assembleRelease 失败,退出代码为 1 - Flutter build apk doesn't work and shows Gradle task assembleRelease failed with exit code 1 从fluttter发布apk后谷歌登录不起作用 - after releasing apk from fluttter google sign in doesn't work 来自 api 的信息未显示 - Flutter - Information from api doesn't show - Flutter Flutter apk 在 android 设备中不起作用 - Flutter apk doesn't work in android device Flutter 在 android studio 3.5 之后,应用程序不会构建 appbundle/APK 或在 Android 上运行 - Flutter App won't build appbundle/APK or run on Android after android studio 3.5 如何从 Flutter 项目构建 APK 文件? - How to build APK file from Flutter project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM