Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在颤振中使isLoading布尔函数为false_Flutter_Flutter Layout - Fatal编程技术网

Flutter 在颤振中使isLoading布尔函数为false

Flutter 在颤振中使isLoading布尔函数为false,flutter,flutter-layout,Flutter,Flutter Layout,我想在我的应用程序中集成颤振连接性检查功能,在集成之后,它对所有小部件都很有效,但在我的情况下,对于setState方法,它不能使用假isLoading方法 class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { bool i

我想在我的应用程序中集成颤振连接性检查功能,在集成之后,它对所有小部件都很有效,但在我的情况下,对于setState方法,它不能使用假isLoading方法

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
      ),
    );
  }
这是我的homeItems小部件,用于checkStatus方法。结果=homeItems()


运行完所有应用程序后,我得到了一个空白屏幕,没有任何错误。请注意解决这个问题。谢谢

使用“最后一次尝试”。最后将isLoading设置为false

您是否尝试执行
checkStatus()然后
\u getRecipe(),在
initState()
中?是。在上面的代码中,我在initState中使用了。不,我的意思是首先调用
checkStatus()
,然后调用
\u getRecipe()
。是的,尝试过了。同样的问题我也试过了。同样的问题
  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");
    }
  }
 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)),
                     ),

                   ],
                 ),
         );
  }
}