[英]Flutter - FutureBuilder error on first build
I am trying to build an app, that displays a list after the call to the database.我正在尝试构建一个应用程序,该应用程序在调用数据库后显示一个列表。 But when I'm launching the app for the first time, there will always be my error message.
但是当我第一次启动该应用程序时,总会有我的错误消息。 The reason why I am posting this in here is: I want that the list shows up even at first launch, so that the error message is not even displaying.
我在此处发布此内容的原因是:我希望即使在第一次启动时也能显示该列表,这样甚至不会显示错误消息。
This is the code of the list:这是列表的代码:
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,
);
}
}
The code of the DBHelper.getRecipes(): 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;
}
The class of 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 returns this: snapshot.error 返回:
NoSuchMethodError: The method 'rawQuery' was called on null.
NoSuchMethodError:在 null 上调用了方法“rawQuery”。 Receiver: null Tried calling: rawQuery('SELECT * FROM recipes')
接收方:null 尝试调用:rawQuery('SELECT * FROM recipes')
The error "NoSuchMethodError The method was called on null" is usually thrown when the variable where the method was called may have yet to be initialized.当调用方法的变量可能尚未初始化时,通常会抛出错误“NoSuchMethodError The method was called on null”。 From the logs you've provided, you're trying to call
rawQuery()
from a null object.从您提供的日志中,您尝试从空对象调用
rawQuery()
。 Verify if _db
has been initialized before calling _db.rawQuery()
.在调用
_db.rawQuery()
之前验证_db
是否已初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.