0

I'm creating a flutter project on which it consist of news feature sadly after building an apk and installing it to try in become an empty screen with grey shade, i also try to test it on my phone and this is what happen i dont know where the bugs came from.. please help me

here's the code


import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'Article_View.dart';
import 'News_Categories.dart';
import 'helper/Data.dart';
import 'helper/News.dart';
import 'model/Article_Model.dart';
import 'model/CategoryModel.dart';

class NewsHomePage extends StatefulWidget {
  @override
  _NewsHomePageState createState() => _NewsHomePageState();
}

class _NewsHomePageState extends State<NewsHomePage> {
  List<CategoryModel> categories = <CategoryModel>[];
  List<ArticleModel> articles = <ArticleModel>[];
  bool _loading = true;
  //bannerads
  late BannerAd _bannerads;
  bool _isAdsLoaded =  false ;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    categories = getCategories();
    getNews();
    _initBannerAds();
  }

  getNews() async {
    News newsClass = News();
    await newsClass.getNews();
    articles = newsClass.news;
    setState(() {
      _loading = false;
    });
  }
  _initBannerAds(){
  _bannerads = BannerAd(
    size: AdSize.banner,
    // ignore: deprecated_member_use
    adUnitId: "ca-app-pub-8634651641429291/4830511818",
    listener: BannerAdListener(
      onAdLoaded: (ad){
        setState(() {
          _isAdsLoaded =true;
        });
      },
      onAdFailedToLoad: (ad,error){}
    ),
    request: const AdRequest()
    );
  _bannerads.load();
 }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.green,
        elevation: 0.0,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Text('Stock '),
            Text(
              'News',
              style: TextStyle(
                color: Colors.black54,
              ),
            ),
          ],
        ),
        centerTitle:true,
    bottom: const PreferredSize(
        preferredSize: Size.zero,
        child: Text("Powered by news.org")),
      ),
      body: _loading
          ? Container(
              child: const Center(
                child: CircularProgressIndicator(),
              ),
            )
          : SingleChildScrollView(
              child: Container(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    ///Categories
                    Container(
                      padding: const EdgeInsets.symmetric(horizontal: 22.0),
                      height: 90.0,
                      child: Expanded(

                        child: ListView.builder(
                            itemCount: categories.length,
                            scrollDirection: Axis.horizontal,
                            shrinkWrap: true,
                            itemBuilder: (context, index) {
                              return CategoryTile(
                                imageUrl: categories[index].imageAssetUrl,
                                categoryName: categories[index].categoryName,
                              );
                            }),
                      ),
                    ),

                    ///Blogs
                    Container(
                      padding: const EdgeInsets.only(top: 16.0),
                      child: Expanded(
                        child: ListView.builder(
                          physics: const ClampingScrollPhysics(),
                          shrinkWrap: true,
                          itemCount: articles.length,
                          itemBuilder: (context, index) {
                            return BlogTile(
                              imageURL: articles[index].urlToImage,
                              title: articles[index].title,
                              desc: articles[index].description,
                              url: articles[index].url,
                            );
                          },
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            bottomNavigationBar: _isAdsLoaded?SizedBox(
      height: _bannerads.size.height.toDouble(),
      width: _bannerads.size.width.toDouble(),
      child: AdWidget(ad: _bannerads),
     )
     :const SizedBox(),
    );
  }
}

class CategoryTile extends StatelessWidget {
  final String imageUrl, categoryName;
  const CategoryTile({required this.imageUrl, required this.categoryName});
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => CategoryNews(
              category: categoryName.toLowerCase(),
            ),
          ),
        );
      },
      child: Container(
        margin: EdgeInsets.only(right: 10.0),
        child: Stack(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: imageUrl != null
                  ? CachedNetworkImage(
                      imageUrl: imageUrl,
                      width: 120,
                      height: 60.0,
                      fit: BoxFit.cover,
                    )
                  : Image.network(
                      imageUrl,
                      width: 120.0,
                      height: 60.0,
                      fit: BoxFit.cover,
                    ),
            ),
            Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.black26,
                borderRadius: BorderRadius.circular(6),
              ),
              width: 120,
              height: 60.0,
              child: Text(
                categoryName,
                style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.w500,
                  fontSize: 18.0,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class BlogTile extends StatelessWidget {
  final String imageURL, title, desc, url;
  BlogTile(
      {required this.imageURL,
      required this.title,
      required this.desc,
      required this.url});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => ArticleView(
                    blogUrl: url,
                  )),
        );
      },
      child: Container(
        margin: const EdgeInsets.only(bottom: 16.0, left: 10.0, right: 10.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(6.0),
              child: imageURL != null
                  ? CachedNetworkImage(
                      imageUrl: imageURL,
                    )
                  : Image.network(imageURL),
            ),
            const SizedBox(
              height: 8.0,
            ),
            Text(
              title,
              style: const TextStyle(
                //color: Colors.black87,
                fontSize: 17.0,
                fontWeight: FontWeight.w500,
              ),
            ),
            const SizedBox(
              height: 8.0,
            ),
            Text(
              desc,
              style: const TextStyle(
                //color: Colors.black54,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

and here's the screen shots of the app [![debugging app][1]][1] [![build app][2]][2] [1]: https://i.stack.imgur.com/tvBsG.jpg [2]: https://i.stack.imgur.com/wOuxS.jpg

1 Answers1

0

Check if this line exists in android/app/src/main/AndroidManifest.xml file. If it doesn't add it right below the package name line. This will grant the app INTERNET permission so it can access it & display the data from the internet.

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

Flutter by default only adds the INTERNET permission in debug mode. When you build an APK in release mode, you have to explicitly add the permission by including the above line.

More info here.

Arpit
  • 110
  • 1
  • 1
  • 7