在抖动中将isLoading布尔函数设为false [英] Make isLoading boolean function to false in flutter

查看:147
本文介绍了在抖动中将isLoading布尔函数设为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中集成Flutter连接检查功能,并且集成后的功能对所有小部件都适用,但对于我来说,对于setState方法,它不能伪造isLoading方法.

I want to integrate flutter connectivity check function in my app, and After integrating it is working great for all widgets but in my case for setState method it can not false isLoading method.

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool isLoading = true;
  Widget result;
  RecipeService _recipeService = RecipeService();
  List<Recipe> _recipeList = List<Recipe>();

  @override
  void initState() {
    super.initState();
    _getRecipe();
    checkStatus();
  }

  _getRecipe() async {
    var dayRecipes = await _recipeService.getRecipeOfTheDay();
    var _list= json.decode(dayRecipes.body);
    List<Recipe> results = [];
    _list['data'].forEach((data) {
      var model = Recipe();
      model.id = data['id'];
      model.title = data['recipeTitle'];
      model.ingredients = data['recipeIngredient'];
      model.directions = data['recipeDirection'];
      model.cookTime = data['cookTime'].toString();
      model.image = data['recipePhoto'];
      results.add(model);
     });
      setState(() {
        _dayRecipeList = results;
        isLoading = false;
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("My Recipe"),
      ),
      body: Container(
        alignment: Alignment.center,
        child: result
      ),
    );
  }

这是Internet的Check方法,在连接的情况下,我使用homeItems()小部件显示我的内容

Here is Check method for internet, where I used homeItems() widget for displaying my content in case of connectivity

  void checkStatus() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.mobile || connectivityResult == ConnectivityResult.wifi) {
      result = homeItems();
      setState(() {});
    } else {
      result = Text("Unable to connect. Please Check Internet Connection");
      setState(() {});
      print("Unable to connect. Please Check Internet Connection");
    }
  }

这是我的homeItems小部件,在checkStatus方法中使用.作为结果= homeItems();

And Here is My homeItems widget, which is used in checkStatus method. as result = homeItems();

 Widget homeItems(){
    Center(
           child: isLoading
               ? CircularProgressIndicator(
                   backgroundColor: Colors.blue,
                   strokeWidth: 10,
                 )
               : ListView(
                   children: [
                      Padding(
                       padding: const EdgeInsets.all(8.0),
                       child: Text("All Recipes",
                           style: TextStyle(
                               fontSize: 25,
                               color: Colors.red,
                               fontWeight: FontWeight.bold)),
                     ),
                     RecipesOfDay(dayRecipeList: _dayRecipeList),
                     Padding(
                       padding: const EdgeInsets.all(8.0),
                       child: Text("All Recipes",
                           style: TextStyle(
                               fontSize: 25,
                               color: Colors.red,
                               fontWeight: FontWeight.bold)),
                     ),

                   ],
                 ),
         );
  }
}

运行所有应用程序后,我得到黑屏,没有任何错误.请看看解决此问题的方法.谢谢

After running all app I am getting blank Screen without any error. Please take a look for solving this issue. Thank You

推荐答案

最后尝试使用.并最终将isLoading设置为false

use try and finally. and set isLoading to false in finally

这篇关于在抖动中将isLoading布尔函数设为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆