簡體   English   中英

Flutter - 首次構建時出現 FutureBuilder 錯誤

[英]Flutter - FutureBuilder error on first build

我正在嘗試構建一個應用程序,該應用程序在調用數據庫后顯示一個列表。 但是當我第一次啟動該應用程序時,總會有我的錯誤消息。 我在此處發布此內容的原因是:我希望即使在第一次啟動時也能顯示該列表,這樣甚至不會顯示錯誤消息。

這是列表的代碼:

import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:side_header_list_view/side_header_list_view.dart';

import 'package:recipe/database/database.dart';
import 'package:recipe/interface/GoogleColors.dart';
import 'package:recipe/model/Recipes.dart';
import 'package:recipe/recipe/recipeDetails.dart';


Future<List<Recipes>> fetchRecipes() async{
  var dbHelper = DBHelper();
  Future<List<Recipes>> recipes = dbHelper.getRecipes();
  return recipes;
}


class Lists extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _List();
  }
}

class _List extends State<Lists>{
  DBHelper db = new DBHelper();
  GoogleMaterialColors colors = new GoogleMaterialColors();
  Random random = new Random(); 
  Color usedColor; 

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

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Container(
        alignment: Alignment.topCenter,        
        child: new FutureBuilder<List<Recipes>>(
          future: fetchRecipes(),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return new Text("Zurzeit sind keine Daten vorhanden."); //this error shows up
            }
            else if (snapshot.hasData) {
              return new ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (BuildContext context, int index){
                  return new Text(snapshot.data[index].name);
                },
              );
            }
            return new Container(alignment: AlignmentDirectional.center,child: new CircularProgressIndicator(),);
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Color(0xFF0F9D58),
        elevation: 4.0,
        child: Icon(Icons.add),
        onPressed: (){
          Navigator.pushNamed(context, '/add_recipe');
        },
      )
    );
  }

  void showBottomSnack(String value, ToastGravity toastGravity){
    Fluttertoast.showToast(
      msg: value,
      toastLength: Toast.LENGTH_SHORT,
      gravity: toastGravity,
      timeInSecForIos: 2,            
    );
  }
}

DBHelper.getRecipes() 的代碼:

Future<List<Recipes>> getRecipes() async{
    List<Map> list = await _db.rawQuery("SELECT * FROM recipes");
    List<Recipes> recipes = new List();
    for(int i =0; i < list.length; i++){
      recipes.add(new Recipes(id: list[i]["id"],name: list[i]["name"],definition: list[i]["definition"],duration:  list[i]["duration"], favorite:  list[i]["favorite"], timestamp: list[i]["timestamp"], image: list[i]["image"],backgroundColor: list[i]["backgroundColor"]));      
    }
    return recipes;
  }

食譜類別:

class Recipes{
  int id, favorite;
  dynamic image;
  String name, definition, timestamp, duration, backgroundColor;

  Recipes(
    {
      @required this.id, 
      this.name, 
      this.definition, 
      this.duration, 
      this.favorite, 
      this.timestamp, 
      this.image, 
      this.backgroundColor
    }
  );
}

未來建造者的錯誤

snapshot.error 返回:

NoSuchMethodError:在 null 上調用了方法“rawQuery”。 接收方:null 嘗試調用:rawQuery('SELECT * FROM recipes')

當調用方法的變量可能尚未初始化時,通常會拋出錯誤“NoSuchMethodError The method was called on null”。 從您提供的日志中,您嘗試從空對象調用rawQuery() 在調用_db.rawQuery()之前驗證_db是否已初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM