Flutter'da isLoading boolean işlevini false yapın

Aug 31 2020

Flutter bağlantı kontrol işlevini uygulamama entegre etmek istiyorum ve entegre ettikten sonra tüm widget'lar için harika çalışıyor ancak benim durumumda setState yöntemi için yanlış isLoading yöntemi olamaz.

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
      ),
    );
  }

Bağlantı durumunda içeriğimi görüntülemek için homeItems () widget'ını kullandığım İnternet için kontrol yöntemi.

  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");
    }
  }

İşte checkStatus yönteminde kullanılan homeItems widget'ım. sonuç olarak = 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)),
                     ),

                   ],
                 ),
         );
  }
}

Tüm uygulamayı çalıştırdıktan sonra herhangi bir hata olmadan boş Ekran alıyorum. Lütfen bu sorunu çözmek için bir göz atın. Teşekkür ederim

Yanıtlar

THEODORE Aug 31 2020 at 18:03

deneyin ve sonunda kullanın. ve sonunda isLoading değerini false olarak ayarlayın