数据无法在Flutter中异步加载

时间:2019-07-07 05:26:48

标签: android flutter dart

在我的flutter应用程序中有两个页面。在一页中,我检查SQLite数据库中是否存在数据,该数据是否已加载到页面中,如果不存在,则数据将从API获取并显示。第二页的过程也与第一页类似。

首页

#include <stdio.h>
#include <string.h>

void extractIpAddress(unsigned char *sourceString,short *ipAddress)
{
     short len=0;
     char  oct[4]={0},cnt=0,cnt1=0,i,buf[5];

     len=strlen(sourceString);
     for(i=0;i<len;i++)
     {
          if(sourceString[i]!='.'){
          buf[cnt++] =sourceString[i];
     }
    if(sourceString[i]=='.' || i==len-1){
        buf[cnt]='\0';
        cnt=0;
        oct[cnt1++]=atoi(buf);
    }
}
ipAddress[0]=oct[0];
ipAddress[1]=oct[1];
ipAddress[2]=oct[2];
ipAddress[3]=oct[3];
}

int main()
{
     unsigned char ip[20]={0};
     short ipAddress[4];

     printf("Enter IP Address (xxx.xxx.xxx.xxx format): ");
     scanf("%s",ip);

     extractIpAddress(ip,&ipAddress[0]);

     printf("\nIp Address: %03d. %03d. %03d. 
     %03d\n",ipAddress[0],ipAddress[1],ipAddress[2],ipAddress[3]);

if(ipAddress[0]>=0 && ipAddress[0]<=127)
    printf("Class A Ip Address.\n");
if(ipAddress[0]>127 && ipAddress[0]<191)
    printf("Class B Ip Address.\n");
if(ipAddress[0]>191 && ipAddress[0]<224)
    printf("Class C Ip Address.\n");
if(ipAddress[0]>224 && ipAddress[0]<=239)
    printf("Class D Ip Address.\n");
if(ipAddress[0]>239)
    printf("Class E Ip Address.\n");

return 0;
}

第二页


class NewsBloc extends BlocBase<NewsStreams, NewsEvents> {
  final NoticeRepository repository;

  DatabaseHelper helper = DatabaseHelper();

  int _page = 0;
  int _limit = 20;
  int _currentCategory;
  Map _categories = Map();
  String searchCat;
  String category;

  List<String> _categoriesNamesSearch = List();
  List<String> _categoriesNames = List();
  List _newsInner = List();

  bool _fnish = true;
  bool _first = true;

  NewsBloc(this.repository) {}

  @override
  void initView() async {
    List categories = await helper.getCategoryList();

    if (categories.isNotEmpty && categories.length > 0) {
      for (var i = 0; i < categories.length; i++) {
        if (categories[i].name == 'සියල්ල') {
          _categoriesNamesSearch.add(categories[i].id);

          _categoriesNames.add(categories[i].name);
          _categories[categories[i].id] = categories[i].name;
          _currentCategory = 0;
        }
      }

      for (var i = 0; i < categories.length; i++) {
        if (categories[i].name != 'සියල්ල') {
          _categoriesNamesSearch.add(categories[i].id);

          _categoriesNames.add(categories[i].name);
          _categories[categories[i].id] = categories[i].name;

        }
      }
      streams.categoriesName.set(_categoriesNames);
      _load(false);

      var categoriesA = await repository.getAllCategory();

      await helper.deleteCategory();
      _categoriesNames = List();
      _categoriesNamesSearch = List();
      for (var i = 0; i < categoriesA.length; i++) {
        await helper.insertCategory(categoriesA[i]);

        if (categoriesA[i].name == 'සියල්ල') {
          _categoriesNamesSearch.add(categoriesA[i].id);

          _categoriesNames.add(categoriesA[i].name);
          _categories[categoriesA[i].id] = categoriesA[i].name;
          _currentCategory = 0;
        }
      }

      for (var i = 0; i < categoriesA.length; i++) {
        if (categoriesA[i].name != 'සියල්ල') {
          _categoriesNamesSearch.add(categoriesA[i].id);

          _categoriesNames.add(categoriesA[i].name);
          _categories[categoriesA[i].id] = categoriesA[i].name;

        }
      }
      streams.categoriesName.set(_categoriesNames);
    } else {
      var categories = await repository.getAllCategory();

      await helper.deleteCategory();
      _categoriesNames = List();
      _categoriesNamesSearch = List();
      for (var i = 0; i < categories.length; i++) {
        await helper.insertCategory(categories[i]);

        if (categories[i].name == 'සියල්ල') {
          _categoriesNamesSearch.add(categories[i].id);

          _categoriesNames.add(categories[i].name);
          _categories[categories[i].id] = categories[i].name;
          _currentCategory = 0;
        }
      }

      for (var i = 0; i < categories.length; i++) {
        if (categories[i].name != 'සියල්ල') {
          _categoriesNamesSearch.add(categories[i].id);

          _categoriesNames.add(categories[i].name);
          _categories[categories[i].id] = categories[i].name;

        }
      }
      streams.categoriesName.set(_categoriesNames);
      _load(false);
    }

  }

  @override
  void eventReceiver(event) async {
    if (event is LoadNews) {
      streams.progress.set(false);
      _first = true;
      _load(false);
    }

    if (event is LoadMoreNews) {

      if (_fnish == true) {
        streams.progress.set(false);
        _fnish = false;
        _load(true);
      }
    }

    if (event is ClickCategory) {

      _newsInner = [];
      _currentCategory = event.data;
      cleanList();
      streams.progress.set(false);
      _load(false);
    }
  }

  _load(bool isMore) async {
    try {

      if (isMore) {
        _page += _limit;
      } else {
        _page = 0;
      }

      streams.errorConection.set(false);

      category = _categoriesNames[_currentCategory];

      searchCat = _categoriesNamesSearch[_currentCategory];

      String toDay = new DateTime.now().toUtc().toIso8601String();

      if (category != 'සියල්ල') {

        var allNws = await helper.getNewsList(searchCat);

        if (_first == true && allNws.length > 0) {
          _showNews(allNws, false);
        }

        streams.progress.set(true);

        var newsSche = await repository.getNewsCat(
            toDay, _page.toString(), searchCat, _limit.toString());

        if (newsSche.length > 0) {
          await helper.deleteNews(_limit);
        }
        if (newsSche.length > 0) {
          for (var i = 0; i < newsSche.length; i++) {
            if (!_newsInner.contains(newsSche[i])) {
              await helper.insertNote(newsSche[i]);
            }
          }
          var allNwsB = await helper.getNewsList(searchCat);
          if (_first == true) {
            _showNews(allNwsB, false);
          } else {
            _showNews(allNwsB, true);
          }

        } else {
          streams.progress.set(false);
        }

        _fnish = true;
        _first = false;

      } else {
        var allNws = await helper.getNewsListNoCat();

        if (_first == true && allNws.length > 0) {
          _showNews(allNws, false);
        }

        streams.progress.set(true);

        var newsSche = await repository.getNewsAll(
            toDay, _page.toString(), _limit.toString());

        if (newsSche.length > 0) {
          await helper.deleteNews(_limit);
        }

        if (newsSche.length > 0) {

          for (var i = 0; i < newsSche.length; i++) {
            if (!_newsInner.contains(newsSche[i])) {
              await helper.insertNote(newsSche[i]);
            }

          }

          var allNwsB = await helper.getNewsListNoCat();
          if (_first == true) {
            _showNews(allNwsB, false);
          } else {
            _showNews(allNwsB, true);
          }
        } else {
          streams.progress.set(false);
        }

        _fnish = true;
        _first = false;

      }

    } catch (e) {
      print("Error$e");
    }
  }

  _showNews(List news, bool isMore) async {
    if (isMore) {
      _newsInner.addAll(news);
    } else {
      _newsInner = news;
    }
    _newsInner.sort((a, b) => b.createdAt.compareTo(a.createdAt));

    if (category != 'සියල්ල') {
       var allNewss = List();
      for (var i = 0; i < _newsInner.length; i++) {
        if (_newsInner[i].parseCategory.runtimeType != String) {
          if (_newsInner[i].parseCategory.id == searchCat) {
            if (!allNewss.contains(_newsInner[i])) {
              allNewss.add(_newsInner[i]);
            }
          }
        } else{
          if(_newsInner[i].parseCategory == searchCat){
            if (!allNewss.contains(_newsInner[i])) {
              allNewss.add(_newsInner[i]);
            }
          }
        }
      }
      streams.noticies.set(allNewss);
    } else {
      var allNewss = List();
      for (var i = 0; i < _newsInner.length; i++) {
        if (_newsInner[i].parseCategory.runtimeType != String) {

            if (!allNewss.contains(_newsInner[i])) {
              allNewss.add(_newsInner[i]);
            }

        } else{

            if (!allNewss.contains(_newsInner[i])) {
              allNewss.add(_newsInner[i]);
            }

        }
      }
      streams.noticies.set(allNewss);
    }
    streams.progress.set(false);

  }

  _showImplError(onError) {
    print(onError);
    if (onError is FetchDataException) {
      print("codigo: ${onError.code()}");
    }
    streams.errorConection.set(true);
    streams.progress.set(false);

  }

  void cleanList() {
    _newsInner = List();
    streams.noticies.set(_newsInner);
  }
}


我的问题是,当我构建应用程序并将其添加到Play商店,并安装到Redmi note7手机时,第一页内容未加载,但第二页内容已加载 仅对于Redmi note 7手机会发生这种情况。请帮助我解决这个问题

0 个答案:

没有答案